Firefox 中出现无效标签错误
当我从另一个域接收一些 json 内容时,我在 Firefox 中收到无效标签错误。我的 JS 代码是:
$.getJSON('http://www.upsidelearning.com/blog/blogapp/getBlogDetails.php?getBlogsList=1&jsoncallback=?', function(data){
alert("Success");
});
并且在 chrome 错误控制台中收到以下错误:
(Warning)Resource interpreted as Script but transferred with MIME type text/html.
(Error) Uncaught SyntaxError: Unexpected token :
编辑:我的响应 json 是:
{"data":[{"title":"How Long Does It Take To Develop An Hour Of Elearning?","publishDate":"15 September 2011","author":"Abhijit Kadle","permalink":"http:\/\/www.upsidelearning.com\/blog\/index.php\/2011\/09\/15\/how-long-does-it-take-to-develop-an-hour-of-elearning\/","thumbnail":"http:\/\/www.upsidelearning.com\/blog\/blogapp\/images\/blogpost-icon.png","id":9197}]}
请帮助我。
I am getting Invalid label error in firefox while receiving some json content from another domain. My JS code is :
$.getJSON('http://www.upsidelearning.com/blog/blogapp/getBlogDetails.php?getBlogsList=1&jsoncallback=?', function(data){
alert("Success");
});
And In chrome error console following error received :
(Warning)Resource interpreted as Script but transferred with MIME type text/html.
(Error) Uncaught SyntaxError: Unexpected token :
EDIT : My response json is :
{"data":[{"title":"How Long Does It Take To Develop An Hour Of Elearning?","publishDate":"15 September 2011","author":"Abhijit Kadle","permalink":"http:\/\/www.upsidelearning.com\/blog\/index.php\/2011\/09\/15\/how-long-does-it-take-to-develop-an-hour-of-elearning\/","thumbnail":"http:\/\/www.upsidelearning.com\/blog\/blogapp\/images\/blogpost-icon.png","id":9197}]}
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是服务器端脚本没有为任何回调添加前缀。你需要解决这个问题。它返回纯
JSON
,如果不将其传递到函数或回调中就无法执行。The problem is that the server side script isn't prefixing any callback. You need to get that fixed. It's returning plain
JSON
which cannot be executed without passing it into a function or callback.由于同源策略限制,不允许将AJAX请求发送到不同的域。有几种解决方法:
使用 JSONP。
getJSON()
文档中有一个关于它的特定部分。仅当远程域支持时才有效。例如,而不是:远程域必须能够返回:
客户端可以在其中设置
回调
名称。如果 JSONP 不是一个选项,您可以在您的域上编写一个服务器端脚本,该脚本将充当您的域和远程域之间的桥梁,然后将 AJAX 请求发送到您的脚本。
如果
更新:
经过大量评论后,您似乎正在尝试使用 YQL。这是一个完整的工作演示:
请注意,您应该使用
$.get
而不是 <代码>$.getJSON。Due to the same origin policy restriction it is not allowed to send AJAX requests to different domains. There are a couple of workarounds:
Use JSONP. There is a specific section about it in the
getJSON()
documentation. Works only if the remote domain supports it. So for example instead of:the remote domain must be able to return:
where the client could set the
callback
name.If JSONP is not an option you could write a server side script on your domain that will act as a bridge between your domain and the remote domain and then send the AJAX request to your script.
UPDATE:
After the numerous comments it seems that you are trying to use YQL. Here's a full working demo:
Notice that you should use
$.get
and not$.getJSON
.