从.json url中提取数据导致Jquery出错

时间:2015-12-16 08:26:46

标签: javascript jquery json ajax

我试图使用一个小脚本来提取数据,我发现它似乎可以与任何其他产生json数据的url一起使用,但是当我使用一个以.json结尾的url时,我只会收到一个语法错误。

//错误

Uncaught SyntaxError: Unexpected token : http://frontier.ffxiv.com/worldStatus/gate_status.json?callback=jQuery21309476937903091311_1450254419566&q=select+title%2Cabstract%2Curl+from+search.news+where+query%3D%22cat%22&format=json&_=1450254419567

//下面的代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
// Using YQL and JSONP
$.ajax({
    url: "http://frontier.ffxiv.com/worldStatus/gate_status.json",

    // The name of the callback parameter, as specified by the YQL service
    jsonp: "callback",

    // Tell jQuery we're expecting JSONP
    dataType: "jsonp",

    // Tell YQL what we want and that we want JSON
    data: {
        q: "select title,abstract,url from search.news where query=\"cat\"",
        format: "json"
    },

    // Work with the response
    success: function( response ) {
        console.log( response ); // server response
    }
});
</script>

1 个答案:

答案 0 :(得分:5)

您执行ajax请求的网址并不提供JSONP,只是普通的JSON。

你得到一个解析错误,因为结果类似于

{"status":0}

而jQuery需要像

这样的东西
callback({"status":0})

不幸的是,它似乎也不支持CORS,因此来自客户端的URL中的数据可能是由于同源策略而产生的。