jQuery + JSONP返回数据为空?

发布于 2024-08-24 01:09:44 字数 696 浏览 2 评论 0原文

我需要访问子域上的数据,我一直在尝试使用 jQuery 支持的 JSONP。我在子域上访问的数据是静态(重新生成)的 .json 文件 (http:// www.example.com/data.json

我遇到了“无效标签错误”错误,并意识到数据需要用括号括起来并使用 ?callback=?

http://www.example.com/data.json?callback=?

({
 "items": [
 {   
  "url": "http://www.example.com",
  "id": "2981",
        "title": "title",
  "description": "lorem ipsum sit dolor",
  "start": "00:10:00",
   "end": "00:20:00"
 }
})


$.getJSON(url, function(data){
 console.log("json: " + data);
});

将数据包装在 () 中是有效的,因为我现在可以看到 Firebug 的 NET 选项卡中返回的数据,但 $.getJSON 不会返回任何内容,我认为它不会触发。

我缺少什么?服务器端还需要做更多的事情吗?

谢谢!

I need to access data on a subdomain I've been trying to use JSONP which jQuery has support for. The data that I'm accessing on the subdomain is a static (regenerated) .json file (http://www.example.com/data.json)

I was running into "Invalid Label Error" errors and realized the data needed to be wrapped in parenthesis and use ?callback=?

http://www.example.com/data.json?callback=?

({
 "items": [
 {   
  "url": "http://www.example.com",
  "id": "2981",
        "title": "title",
  "description": "lorem ipsum sit dolor",
  "start": "00:10:00",
   "end": "00:20:00"
 }
})


$.getJSON(url, function(data){
 console.log("json: " + data);
});

Wrapping the data in () worked as I'm now able to see the data returned in the NET tab of Firebug, but the $.getJSON doesn't return anything, I don't think it fires.

What am I missing? Does something more need to be done server-side?

Thanks!

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

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

发布评论

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

评论(3

小糖芽 2024-08-31 01:09:44

您返回的数据不是 JSON-P。

维基百科对 JSON-P 的外观有一个很好的解释。有 接受 jQuery 特定指南JSON-P

The data you are returning isn't JSON-P.

Wikipedia has a decent explanation of what JSON-P should look like. There is a jQuery specific guide to accepting JSON-P.

奈何桥上唱咆哮 2024-08-31 01:09:44

您从 HTTP 请求返回的代码正在由浏览器执行。

浏览器执行得很好,但它不知道把它放在哪里,因为它没有分配给任何东西。

幸运的是,jQuery 足够智能,让您可以在代码中使用参数 (callback=?)。

您的服务器端语言必须添加该回调参数并使您的 JSON 响应看起来像 JavaScript 函数调用:

<?php echo $_GET["callback"]?>({
 "items": [
 {   
  "url": "http://www.example.com",
  "id": "2981",
        "title": "title",
  "description": "lorem ipsum sit dolor",
  "start": "00:10:00",
   "end": "00:20:00"
 }
})

The code that you're returning from the HTTP request is being executed by the browser.

The browser executes it fine, but it doesn't know where to put it because it isn't assigned to anything.

Fortunately jQuery is smart enough, and let's you use a parameter (callback=?) for you to use in your code.

Your server side language must add that callback parameter and make your JSON response look like a JavaScript function call:

<?php echo $_GET["callback"]?>({
 "items": [
 {   
  "url": "http://www.example.com",
  "id": "2981",
        "title": "title",
  "description": "lorem ipsum sit dolor",
  "start": "00:10:00",
   "end": "00:20:00"
 }
})
朦胧时间 2024-08-31 01:09:44

如果您尝试在 http://jsonlint.com/ 上验证 JSON 数据,您会发现存在一些错误。

您是通过框架还是手动生成 JSON 数据。

手动生成 JSON 数据很容易出错。

我希望能帮助你纠正你的错误。

马丁

if you try to validate your JSON data for example on http://jsonlint.com/ you see that there are some errors.

Do you generate the JSON data by a framework or manually.

Manual JSON data generation is error prone.

I hope helps you to fix your error.

Martin

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