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

Pulling data from .json url causing error with Jquery

本文关键字:Jquery 出错 数据 提取 json url      更新时间:2023-09-26

我正在尝试使用一个小脚本来提取数据,我发现这个脚本似乎可以与任何其他生成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>

您进行ajax请求的URL不提供JSONP,只提供常规JSON。

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

{"status":0}

而jQuery期望类似的东西

callback({"status":0})

不幸的是,看起来也不支持CORS,因此由于同源策略,无法从客户端获取该URL中的数据。