如何获取失败的ajax的错误消息

时间:2012-06-05 11:52:20

标签: javascript jquery json

我想使用一个webService,我从

获取JSON数据
    $.ajax({
    type: 'GET',
    contentType: 'application/json',
    dataType: 'JSON-P',
    //the url shows the correct JSON Data when I copy the URL in my browser
    url: 'http://url.com',
    success: function(data){
        console.log(data);
        alert(data);
    },
       error: function(xhr, testStatus, error){console.log("Error occured: "+error+" "+xhr+" "+testStatus)}
})

在Firefox中,它使用错误函数回调,但我不知道问题是什么,知道错误消息会很好,但我的方法不起作用。 提前谢谢

编辑:在Chrome中,我收到以下错误:

  

Access-Control-Allow-Origin不允许使用原点。

编辑:已经解决了,问题确实是json不能跨站点工作,并且数据不是“jsonp-conform”(它必须设置一个函数(json数据......))围绕json数据。这可以通过更改末尾的URL来完成“_& jsonp = successCallback。抱歉打扰你

2 个答案:

答案 0 :(得分:1)

我认为存在JSON对象语法错误。使用jsonlint.com检查JSON对象语法错误。

答案 1 :(得分:0)

您是否在本地收到此错误?

在服务器上试用它,如果它不起作用:XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

相关问题