无法向 jquery getJSON 添加回调函数

发布于 2024-12-12 04:19:15 字数 1244 浏览 0 评论 0原文

我正在尝试在 jquery 中执行跨域请求,在类似于 this gist 中找到的函数

中我以为我的 JSON 不正确,但这就是我从响应中得到的:

{"status":"OK","errorMessage":"","numberOfResults":10,"suggestions":[{"url":"http://dbpedia.org/resource/Bassiana","label":"Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Julia_Soaemias_Bassiana","label":"Julia Soaemias Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Julia_Bassiana","label":"Julia Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Varius_Avitus_Bassianus_Marcus_Aurelius_Antoninus","label":"Varius Avitus Bassianus Marcus Aurelius Antoninus","owner":0},{"url":"http://dbpedia.org/resource/Bassianus_%28senator%29","label":"Bassianus (senator)","owner":0},{"url":"http://dbpedia.org/resource/Johannes_Bassianus","label":"Johannes Bassianus","owner":0},{"url":"http://dbpedia.org/resource/Julius_Bassianus","label":"Julius Bassianus","owner":0},{"url":"http://dbpedia.org/resource/Bassian_thrush","label":"Bassian thrush","owner":0},{"url":"http://dbpedia.org/resource/Bassianae","label":"Bassianae","owner":0},{"url":"http://dbpedia.org/resource/Bassian","label":"Bassian","owner":0}]}

我应该如何使其工作?由于 JSON 无效,回调似乎从未被触发。

I am trying to do a cross domain request within jquery, in a function like the one found in this gist

I thought I had incorrect JSON, but this is what I get from the response:

{"status":"OK","errorMessage":"","numberOfResults":10,"suggestions":[{"url":"http://dbpedia.org/resource/Bassiana","label":"Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Julia_Soaemias_Bassiana","label":"Julia Soaemias Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Julia_Bassiana","label":"Julia Bassiana","owner":0},{"url":"http://dbpedia.org/resource/Varius_Avitus_Bassianus_Marcus_Aurelius_Antoninus","label":"Varius Avitus Bassianus Marcus Aurelius Antoninus","owner":0},{"url":"http://dbpedia.org/resource/Bassianus_%28senator%29","label":"Bassianus (senator)","owner":0},{"url":"http://dbpedia.org/resource/Johannes_Bassianus","label":"Johannes Bassianus","owner":0},{"url":"http://dbpedia.org/resource/Julius_Bassianus","label":"Julius Bassianus","owner":0},{"url":"http://dbpedia.org/resource/Bassian_thrush","label":"Bassian thrush","owner":0},{"url":"http://dbpedia.org/resource/Bassianae","label":"Bassianae","owner":0},{"url":"http://dbpedia.org/resource/Bassian","label":"Bassian","owner":0}]}

How am I supposed to make it work? It seems the callback is never fired since the JSON is not valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-12-19 04:19:15

问题不在于它是无效的(因为通过 此站点 传递它会确认),而是它从未“包装”在一个回调方法。

查找 JSONp

如果您要调用 /getMyJSON?callback=myCallback 您的 JSONP 响应应该返回如下:(

myCallback({"status":"OK","errorMessage":"","numberOfResults" ...);

请注意,它现在包装在一个函数调用中,您应该在您的页面,并准备处理返回的结果)。

The problem is not that it's invalid (as passing it though this site will confirm), it's that it's never "wrapped" in a callback method.

Look up JSONp.

If you were to call /getMyJSON?callback=myCallback your JSONP response should come back like:

myCallback({"status":"OK","errorMessage":"","numberOfResults" ...);

(Note that it's now wrapped in a function call that you should have defined on your page, and ready for processing the returned results).

太傻旳人生 2024-12-19 04:19:15

跨域请求需要使用JSONP。 JSONP 请求实际上作为脚本请求发送,并要求您提供回调(和回调参数),以便服务器可以传递一个脚本,其中包含对回调函数的调用作为其内容。使用 getJSON 调用执行此操作的方法是将 &callback=? 附加到其他 URL 参数。这向 jQuery 表明它必须发出 JSONP 请求,并指示它创建一个调用回调的匿名函数。

注意:服务器必须支持 JSONP,因为它需要以不同的方式处理响应,即以脚本形式返回,并将数据作为回调的参数提供。支持通过 Javascript 直接访问的服务应该支持 JSONP。如果服务不支持 JSONP,您需要在服务器上代理请求,您可以在服务器上以纯 JSON 形式请求它,因为您没有跨域浏览器限制。

$.getJSON('http://some.other.dom/controller/action?x=y&callback=?`, function(data) {
    // here the data is the deserialized JSON as an object
});

A cross domain request needs to use JSONP. JSONP requests are actually sent as a script request and require that you supply a callback (and the callback parameter) so that the server can deliver a script which contains a call to your callback function as it's content. The way to do this with the getJSON call is to append &callback=? to your other URL parameters. This notes to jQuery that it must make a JSONP request and also instructs it to create an anonymous function that invokes your callback.

NOTE: the server must support JSONP as it needs to handle the response differently, i.e., returned as a script with the data supplied as the parameter to the callback. Services that support direct access via Javascript should support JSONP. If the service doesn't support JSONP, you'll need to proxy the request on your server, where you can request it as plain JSON since you don't have the cross domain browser restrictions.

$.getJSON('http://some.other.dom/controller/action?x=y&callback=?`, function(data) {
    // here the data is the deserialized JSON as an object
});
十二 2024-12-19 04:19:15

那不是 jsonp - jsonp 包含回调。您看到的要点没有提到跨域。您必须查看该服务并了解需要执行哪些操作才能使其返回 jsonp 而不是 json。

下面是一个返回我最后一个 twitter feed 的示例调用:

http://twitter.com/status/user_timeline/rossdargan.json?count=1&callback=JSONPcallbackFunction

如您所见,结果被包装在 JSONPcallbackFunction 中:

JSONPcallbackFunction([{"place":{"bounding_box":{"type":"Polygon","coordinates...

Thats not jsonp - jsonp includes the callback. The gist you looked at says nothing about being cross domain. You will have to look at the service and see what you need to do to get it to return jsonp not json.

Here is a sample call returning my last twitter feed:

http://twitter.com/status/user_timeline/rossdargan.json?count=1&callback=JSONPcallbackFunction

As you see the result gets wrapped in the JSONPcallbackFunction:

JSONPcallbackFunction([{"place":{"bounding_box":{"type":"Polygon","coordinates...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文