gpt4 book ai didi

javascript - 通过 ajax 调用 Web 服务 - 在我的错误回调中的正确响应

转载 作者:可可西里 更新时间:2023-11-01 02:47:33 24 4
gpt4 key购买 nike

我正在尝试使用以下函数通过 ajax 从 Web 服务获取一些数据,但我收到了这条回复信息:

{"readyState":4, "status":200, "statusText":"load"} 

WS 应该返回一个 json 数组,如果我查看我的 chrome 开发工具在网络选项卡 -> 响应中,我实际上得到了正确的 json 数组。

问题:

  1. 为什么我在 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 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*

最佳答案

您看到 error 回调被触发,因为您的 AJAX 请求有问题,并且没有成功返回。确定为什么会发生这种情况是另一回事。

jQuery 传递给您的error 回调的第一个参数是jqXHR object。 :

error 
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

这与 success 回调不同,后者以返回的 data 开始:

success
Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )

jqXHRxmlHttpRequest 的超集对象 JavaScript 返回。在其中,您看到 readyState 为 4,这仅表示“完成”,而 status 为 200 表示请求成功。因此,至少您知道您可能将您的请求指向正确的 URL。

您应该能够从您的 jqXHR 对象中获取其他信息,这可能有助于您确定错误的原因。来自文档:

For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:

  • readyState
  • status
  • statusText
  • responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
  • setRequestHeader(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 one
  • getAllResponseHeaders()
  • getResponseHeader()
  • statusCode()
  • abort()

关于javascript - 通过 ajax 调用 Web 服务 - 在我的错误回调中的正确响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com