简体   繁体   English

什么/什么时候调用 jQuery AJAX 方法返回?

[英]What/when does a call to the jQuery AJAX method return?

A little background:一点背景:

I am trying to implement and AJAX powered SlickGrid.我正在尝试实施 AJAX 供电的 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:在此示例中,有以下代码可以访问所需的 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;

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.我不确定 jsonp 做了什么,但从我读到的内容来看,它似乎与 jQuery 中的 ajax 方法非常相似,只是它返回 json 并允许跨域请求。 The webservice that I happen to be calling only returns XML so I changed this chunk of code to:我碰巧调用的 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;

My issue is that my page errors out at req.fromPage = fromPage;我的问题是我的页面错误出现在req.fromPage = fromPage; because req is null.因为req是 null。

Am I wrong to think that I can just replace my jsonp call with a call to the ajax method?我认为我可以用对 ajax 方法的调用来替换我的 jsonp 调用是错误的吗? Is req just not set because my ajax call hasn't finished by the time that code is executed? req 只是没有设置,因为我的 ajax 调用在代码执行时尚未完成? 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.如果我注释掉最后两行并将这些值硬编码到其他地方,一切都会运行良好。

Am I wrong to think that I can just replace my jsonp call with a call to the ajax method?我认为我可以用对 ajax 方法的调用来替换我的 jsonp 调用是错误的吗?

No, that should work just fine.不,那应该工作得很好。

Is req just not set because my ajax call hasn't finished by the time that code is executed? req 只是没有设置,因为我的 ajax 调用在代码执行时尚未完成?

Yes, that is correct.对,那是正确的。

The ajax methods starts the request and returns immediately. ajax 方法启动请求并立即返回。 If you want to do something after the response has arrived you should do that in the success event handler.如果您想在响应到达后执行某些操作,您应该在success事件处理程序中执行此操作。

You might actually want to use the success event instead of the complete event, as the complete event happens even if there is an error.您实际上可能希望使用success事件而不是complete事件,因为即使出现错误也会发生complete事件。

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.可以在设置中指定async: false,以使 ajax 调用等待响应,但这意味着浏览器在等待时会冻结。

As Guffa stated , $.ajax() works asynchronically.正如Guffa 所说$.ajax()异步工作。 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.因此,您必须指定在请求返回响应时将调用的回调,而不是仅使用$.ajax()返回的任何内容。

There are a couple of different callback methods you can specify:您可以指定几种不同的回调方法:

  • complete - runs when you recieve a response, regardless of its status. complete - 当您收到响应时运行,无论其状态如何。
  • success - runs when you recieve a response with a successful status code (usually 200). success - 当您收到带有成功状态码(通常为 200)的响应时运行。
  • error - runs when you recieve a response with an error code (for example 404 or 500). error - 当您收到带有错误代码(例如 404 或 500)的响应时运行。

To do something with the response body after a successful request, you should do something like要在请求成功后对响应正文执行某些操作,您应该执行类似的操作

$.ajax({
    ...
    success: function(body) {
        alert('This is the method body:' + body);
    }
});

Read up in the documentation on the different methods to see what more parameters you can use.阅读有关不同方法的文档,以了解您可以使用哪些更多参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM