对 jQuery AJAX 方法的调用什么时候返回?
一点背景知识:
我正在尝试实现 AJAX 支持的 SlickGrid。没有太多文档,所以我使用这个示例作为基础。
在此示例中,以下代码可访问所需的 Web 服务以获取数据:
req = $.jsonp({
url: url,
callbackParameter: "callback",
cache: true, // Digg doesn't accept the autogenerated cachebuster param
success: onSuccess,
error: function(){
onError(fromPage, toPage)
}
});
req.fromPage = fromPage;
req.toPage = toPage;
我不太确定 jsonp 的作用,但从我读到的内容来看,它似乎与 jQuery 中的 ajax 方法非常相似,除了它返回 json 并允许跨域请求。我碰巧调用的 Web 服务仅返回 XML,因此我将这段代码更改为:
req = $.ajax({
url: "/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: xmlData,
complete: onSuccess,
error: function (xhr, ajaxOptions, thrownError) {
alert("error: " + xhr.statusText);
alert(thrownError);
},
contentType: "text/xml; charset=\"utf-8\""
});
req.fromPage = fromPage;
req.toPage = toPage;
我的问题是我的页面在 req.fromPage = fromPage;
处出错,因为 req
为空。
我是否错误地认为我可以用对 ajax 方法的调用替换我的 jsonp 调用? req 是否只是因为代码执行时我的 ajax 调用尚未完成而未设置?我怎样才能解决这些问题?
如果我注释掉最后两行并在其他地方对这些值进行硬编码,一切都会正常运行。
A little background:
I am trying to implement and AJAX powered SlickGrid. There isn't much documentation so I used this example as a base.
In this example there is the following code that hits the desired web service to get the data:
req = $.jsonp({
url: url,
callbackParameter: "callback",
cache: true, // Digg doesn't accept the autogenerated cachebuster param
success: onSuccess,
error: function(){
onError(fromPage, toPage)
}
});
req.fromPage = fromPage;
req.toPage = toPage;
I'm not exactly sure what jsonp does but from what i've read it appears to be very similar to the ajax method in jQuery except it returns json and allows cross domain requests. The webservice that I happen to be calling only returns XML so I changed this chunk of code to:
req = $.ajax({
url: "/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: xmlData,
complete: onSuccess,
error: function (xhr, ajaxOptions, thrownError) {
alert("error: " + xhr.statusText);
alert(thrownError);
},
contentType: "text/xml; charset=\"utf-8\""
});
req.fromPage = fromPage;
req.toPage = toPage;
My issue is that my page errors out at req.fromPage = fromPage;
because req
is null.
Am I wrong to think that I can just replace my jsonp call with a call to the ajax method? Is req just not set because my ajax call hasn't finished by the time that code is executed? How can I get around either of these issues?
If I comment out the last two lines and hard-code those values elsewhere everything runs fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,那应该很好用。
是的,这是正确的。
ajax 方法启动请求并立即返回。如果您想在响应到达后执行某些操作,则应该在
success
事件处理程序中执行该操作。您实际上可能想使用
success
事件而不是complete
事件,因为即使出现错误,complete
事件也会发生。您可以在设置中指定
async: false,
以使ajax调用等待响应,但这意味着浏览器在等待时会冻结。No, that should work just fine.
Yes, that is correct.
The ajax methods starts the request and returns immediately. If you want to do something after the response has arrived you should do that in the
success
event handler.You might actually want to use the
success
event instead of thecomplete
event, as thecomplete
event happens even if there is an error.You could specify
async: false,
in your settings to make the ajax call wait for the response, but that means that the browser freezes while it's waiting.作为 Guffa 声明,
$.ajax()
有效异步地。因此,您必须指定一个在请求返回响应时调用的回调,而不是仅使用$.ajax()
返回的内容。您可以指定几种不同的回调方法:
complete
- 在收到响应时运行,无论其状态如何。success
- 当您收到带有成功状态代码(通常为 200)的响应时运行。error
- 当您收到带有错误代码(例如 404 或 500)的响应时运行。要在成功请求后对响应正文执行某些操作,您应该执行类似
阅读 文档< /a> 在不同的方法上查看您还可以使用哪些参数。
As Guffa stated,
$.ajax()
works asynchronically. Thus, you have to specify a callback that will be called when the request has returned a response, rather than to just use whatever$.ajax()
returns.There are a couple of different callback methods you can specify:
complete
- runs when you recieve a response, regardless of its status.success
- runs when you recieve a response with a successful status code (usually 200).error
- runs when you recieve a response with an error code (for example 404 or 500).To do something with the response body after a successful request, you should do something like
Read up in the documentation on the different methods to see what more parameters you can use.