jQuery服务响应显示在firebug但没有成功

时间:2013-06-08 15:04:45

标签: jquery ajax json

我在使用浏览器访问服务网址时遇到问题,并在浏览器上显示json结果。

  /*-secure-{"statusCode":200,"errors":
  ],"isSuccessful":true,"statusReason":"OK","Envelope":
 {"Body":{"GetStoresByZipcodeResponseElement":
 {"ns1":"http:\/\/abc.com\/intg\/ws\/\/provider","status":"statusCode":"000","statusDescription":"Success"}}*/"

但是当我使用jquery点击该url时:

 $.ajax({
        url: url,
        cache: true,
        dataType: 'script',
        type: 'GET',
        async: false, // must be set to false
        success: function (data, success) {
           console.log(" success "+ JSON.stringify(success));
           console.log(" data " +JSON.stringify(data));
        },
        error :function( jqxhr, textStatus, error ) { 
        var err = textStatus + ', ' + error;
        console.log( "Request Failed: " + err);
        },

        complete: function (jqxhr, textStatus ){
        console.log( "complete: " + JSON.stringify(jqxhr)+" "+ textStatus );
        }
    });

它向我展示了萤火虫的反应,但即使它成为了ajax的成功函数,而且数据也未定义。

用'json'更改'script'之后

返回:

Request Failed: error,
complete: {"readyState":0,"responseText":"","responseJSON":null,"status":0,"statusText":"error"} error

Firebug中的JSON响应和响应选项卡视图 JSON Response in Firebug Response in Firebug

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试使用这些回调函数,找出“数据”是什么。

success: function(data, success) {
  console.log("success", arguments);
  console.log("data", typeof data, data); // Verify the response
},
error: function(jqxhr, textStatus, error) { 
  console.log("error", arguments);
},
complete: function(jqxhr, textStatus) {
  console.log("complete", arguments);
}

另外,为什么不在调试时禁用缓存? cache: false

似乎您正在尝试请求一些secured JSON数据,因为响应不是有效的JSON,这可能是jQuery无法解析它的原因。

如果需要,可以编写自定义dataFilter来修剪和解析响应。

dataFilter: function(data, type) {

  if (type === 'json') {
    // TODO: Parse the custom format by removing the comments
    // and then parsing what is expected to be valid JSON.
    return $.parseJSON(data);
  }

  return data;
}

<强>更新

您正在尝试发出跨域请求,并且应该使用JSONP,如果服务提供商支持它,因为您受到Same Origin Policy的限制。

相关问题