在Firefox中获取无效标签错误

时间:2011-09-19 06:07:03

标签: jquery ajax json cross-domain jsonp

我在从另一个域接收一些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}]}

请帮帮我。

2 个答案:

答案 0 :(得分:1)

  

我在从另一个域接收一些json内容时在firefox中收到无效标签错误。

由于same origin policy限制,不允许将AJAX请求发送到不同的域。有几种解决方法:

  1. 使用JSONP。 getJSON()文档中有关于它的特定部分。仅在远程域支持时才有效。例如,而不是:

    {"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}]}
    

    远程域必须能够返回:

    callback({"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}]})
    

    客户端可以设置callback名称。

  2. 如果JSONP不是一个选项,您可以在您的域上编写一个服务器端脚本,作为您的域和远程域之间的桥梁,然后将AJAX请求发送到您的脚本。

    < / LI>

    更新:

    在众多评论之后,您似乎正在尝试使用YQL。这是一个full working demo

    $.get('http://www.upsidelearning.com/blog/blogapp/getBlogDetails.php?getCategories=1&callback=?', function(result) {
        // you could parse the JSON like this:
        // var json = $.parseJSON(($(result.responseText).filter('p').text()));
        // alert(json.data[0].id);
    
        alert("Success");
    });
    

    请注意,您应该使用$.get而不是$.getJSON

答案 1 :(得分:1)

问题是服务器端脚本没有为任何回调添加前缀。你需要修复它。它返回普通JSON,如果不将它传递给函数或回调函数就无法执行。