我试图通过以下功能通过ajax从Web服务获取一些数据,
但我收到这个回复消息:
但我收到这个回复消息:
{"readyState":4,"status":200,"statusText":"load"}
WS应该返回一个json数组,如果我查看我的chrome开发工具
在网络标签 – >响应,我实际上得到了适当的json数组.
题:
为什么我的结果在我的errorFunction回调?
function callWebService(wsUrl,params,successFunction,errorFunction) { $.ajax({ beforeSend: function (xhr) { xhr.setRequestHeader('Access-Control-Allow-Methods',' GET'); xhr.setRequestHeader("Content-Type","application/json; charset=utf-8"); xhr.setRequestHeader("Accept","application/json"); },type: "GET",url: wsUrl,data: params,dataType: "json",contentType: "application/json",success: successFunction,error: errorFunction }); }
这是我的console.log当我使用这个错误函数函数(jqXHR,status,error)
*Resource interpreted as Script but transferred with MIME type text/html: "http://www.myweb.it/services/service.PHP?callback=jQu…y21109160579217132181_1405523828314&codice_istituto=150201&_=1405523828315". jquery.js:8691send jquery.js:8691jQuery.extend.ajax jquery.js:8152callWebService global.js:5(anonymous function) index.js:49jQuery.event.dispatch jquery.js:4409elemData.handle jquery.js:4095 an error occurred: index.js:52 parsererror index.js:53 Error {stack: (...),message: "jQuery21109160579217132181_1405523828314 was not called"}message: "jQuery21109160579217132181_1405523828314 was not called"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: d index.js:54 readyState: 4 index.js:56 jqXHR.status: 200 index.js:57 jqXHR.statusText:load index.js:58 jqXHR.responseText: undefined*
解决方法
您看到错误回调被触发,因为您的AJAX请求有问题,并且它没有成功返回.确定为什么会发生这样的事情是另一回事.
第一个参数是jQuery传递给你的错误回调是jqXHR object:
error Type: Function( jqXHR jqXHR,String textStatus,String errorThrown )
这与成功回调有所不同,它从返回的数据开始:
success Type: Function( PlainObject data,jqXHR jqXHR )
jqXHR是xmlHttpRequest
对象JavaScript的超集.在它里面,你看到readyState of 4,它只是意味着“完成”,状态200表示一个成功的请求.所以,至少你知道你可能会把你的请求指向正确的URL.
您应该可以从jqXHR对象获取其他信息,这可能有助于您识别错误的原因.从文档:
For backward compatibility with XMLHttpRequest,a jqXHR object will
expose the following properties and methods:
readyState
status
statusText
responseXML
and/orresponseText
when the underlying request responded with xml and/or text,respectivelysetRequestHeader(name,value)
which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old onegetAllResponseHeaders()
getResponseHeader()
statusCode()
abort()