未在 Jquery.GetJSON 中获取数据
我有一个使用这个 url 的工作示例
http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=jQuery16201154390876987067_1314382298849&tags=cat&tagmode=any&format=json&_=1314382298856
,它给了我这个 json 结果(这是它的开始:)
jQuery162031768042474373037_1314374838725({
"title": "Recent Uploads tagged cat",
"link": "http://www.flickr.com/photos/tags/cat/",
当我将其粘贴到 jslint 中时,它说它不是有效的 json
现在我有自己的 json 服务,它返回以下内容:
{
"one0": "file201101_01.jpg ",
"one1": "file201101_02.jpg ",
"one2": "file201101_03.jpg ",
"one3": "file201101_04.jpg "
}
根据 jslint,这是有效的 json。
现在第一个(似乎无效)由 jquery.getJSON 读取,但我的(似乎有效)不是。
当我查看 Firebug 时,它“说”我的服务没有返回任何内容,但是当我将其复制粘贴到浏览器中时,显示的 URL 会返回内容。
这是我的代码:
$.getJSON("http://jadieda.com/myservice.php",
{ year: "2011",
id : "1"
},
function(data)
{
alert(data);
});
警报(数据)不会消失,所以我的猜测是服务的调用没有返回有效的 json (因为来自帮助:从 jQuery 1.4 开始,如果 JSON 文件包含语法错误,请求通常会默默失败)
i have a working example which uses this url
http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=jQuery16201154390876987067_1314382298849&tags=cat&tagmode=any&format=json&_=1314382298856
which gives me this json result (this is the beginning of it:)
jQuery162031768042474373037_1314374838725({
"title": "Recent Uploads tagged cat",
"link": "http://www.flickr.com/photos/tags/cat/",
When i paste this in jslint, it says it is not valid json
now i have my own json service, which returns this:
{
"one0": "file201101_01.jpg ",
"one1": "file201101_02.jpg ",
"one2": "file201101_03.jpg ",
"one3": "file201101_04.jpg "
}
which is valid json according to jslint.
now the first (seems to be invalid) one is read by jquery.getJSON, but mine (which seems to be valid) isn't.
when i look in firebug, it 'says' that no content is returnd from my service, but the url shown is returning content when i copy-paste it in the browser.
This is my code:
$.getJSON("http://jadieda.com/myservice.php",
{ year: "2011",
id : "1"
},
function(data)
{
alert(data);
});
the alert(data) doesn't go off, so my guess was that the calling of the service did not return valid json (because of this from the help:As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 JSONP,这是一种获取跨域 AJAX 的方法。
我不知道为什么你的服务在没有看到 PHP 的情况下不返回数据。但如果状态码是204,则没有内容。那么很可能您没有
echo/print
任何数据;That's JSONP which is a way of getting cross domain AJAX.
I don't know why your service isn't returning data without seeing the PHP. But if the status code is 204, no content. Then chances are you are not
echo/print
any data;