eea*_*dev 10 javascript ajax jquery json
我试图通过ajax使用下面的函数从Web服务获取一些数据,但我收到此响应消息:
{"readyState":4, "status":200, "statusText":"load"}
Run Code Online (Sandbox Code Playgroud)
WS应该返回一个json数组,如果我查看网络选项卡中的chrome dev工具 - > Response,我实际上得到了正确的json数组.
题:
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
});
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个错误函数时,这是我的console.log function(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*
Run Code Online (Sandbox Code Playgroud)
您看到error回调被触发,因为您的 AJAX 请求出现问题,并且没有成功返回。确定为什么会发生这种情况是另一回事。
jQuery 传递给回调的第一个参数error是jqXHR 对象:
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
Run Code Online (Sandbox Code Playgroud)
这与回调不同success,回调以data返回的内容开始:
success
Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )
Run Code Online (Sandbox Code Playgroud)
jqXHR是 JavaScript 返回的对象的超集xmlHttpRequest。在其中,您会看到readyState4,它仅表示“完成”,而status200 表示请求成功。因此,至少您知道您可能将请求指向了正确的 URL。
您应该能够从jqXHR对象中获取其他信息,这可能有助于您确定错误的原因。来自文档:
为了向后兼容 XMLHttpRequest,jqXHR 对象将公开以下属性和方法:
readyStatestatusstatusTextresponseXML和/或responseText当底层请求分别以 xml 和/或文本响应时setRequestHeader(name, value)它偏离了标准,用新值替换旧值,而不是将新值连接到旧值getAllResponseHeaders()getResponseHeader()statusCode()abort()