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

发布于 2024-11-18 08:34:43 字数 1624 浏览 4 评论 0原文

一点背景知识:

我正在尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枉心 2024-11-25 08:34:43

我是否错误地认为我可以用对 ajax 方法的调用替换我的 jsonp 调用?

不,那应该很好用。

req 是否只是因为代码执行时我的 ajax 调用尚未完成而未设置?

是的,这是正确的。

ajax 方法启动请求并立即返回。如果您想在响应到达后执行某些操作,则应该在 success 事件处理程序中执行该操作。

您实际上可能想使用 success 事件而不是 complete 事件,因为即使出现错误,complete 事件也会发生。

可以在设置中指定async: false,以使ajax调用等待响应,但这意味着浏览器在等待时会冻结。

Am I wrong to think that I can just replace my jsonp call with a call to the ajax method?

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?

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 the complete event, as the complete 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.

宫墨修音 2024-11-25 08:34:43

作为 Guffa 声明$.ajax()有效异步地。因此,您必须指定一个在请求返回响应时调用的回调,而不是仅使用 $.ajax() 返回的内容。

您可以指定几种不同的回调方法:

  • complete - 在收到响应时运行,无论其状态如何。
  • success - 当您收到带有成功状态代码(通常为 200)的响应时运行。
  • error - 当您收到带有错误代码(例如 404 或 500)的响应时运行。

要在成功请求后对响应正文执行某些操作,您应该执行类似

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

阅读 文档< /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

$.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文