简体   繁体   English

在jQuery中如何发送一个ajax请求后调用一个函数?

[英]in jquery how to call a function after an ajax request has been sent?

How can I call my function: myFunc1 in jquery after the browser has completely sent an ajax request? 浏览器完全发送ajax请求后, myFunc1jquery调用我的函数: myFunc1

I want to run a callback function after a jquery ajax has been sent . 我想在发送了jQuery ajax之后运行回调函数。 I don't want to do this on complete or success because I don't want to wait for the request to complete. 我不想completesuccess执行此操作,因为我不想等待请求完成。 I want to fire it right after the ajax request has been sent from the browser. 我想在浏览器发送了ajax请求后立即将其触发。

This is use case for which i need this: 这是我需要的用例:

I want run a function after making an ajax request. 我想在发出ajax请求后运行一个函数。 My original code was this: 我的原始代码是这样的:

  $.ajax({url: 'x.html'});
  myFunc1();

In the case when `myFunc1() changes the browsers location, the browser would cancel the pending ajax request. 在`myFunc1()更改浏览器位置的情况下,浏览器将取消待处理的ajax请求。

My current solution is to do this: 我当前的解决方案是这样做:

  $.ajax({url: 'x.html', complete: myFunc1});

This makes sure that myFunc1 is not called till the ajax is complete. 这样可以确保在ajax完成之前不调用myFunc1 Although this solution works, it is inefficient because myFunc1 wont run till the request is complete. 尽管此解决方案有效,但效率不高,因为myFunc1在请求完成myFunc1不会运行。 Since I don't care what the ajax request returns, whether it fails or succeeds, I dont need to wait for it to complete to run the myFunc1 function. 由于我不在乎ajax请求返回什么,无论它失败还是成功,所以我不需要等待它完成就可以运行myFunc1函数。

If you look at the specification: 如果您查看规格:

http://www.w3.org/TR/XMLHttpRequest/ http://www.w3.org/TR/XMLHttpRequest/

You'll see 2 means the headers have been returned already. 您会看到2表示标题已经返回。

const unsigned short **HEADERS_RECEIVED** = 2;

This is essentially the same as jqXHR success. 这与jqXHR的成功基本相同。 Your browser may vary =) 您的浏览器可能不同=)

No seriously.. they will vary, another reason jquery can't support them all and chooses to abstract them: 不严重..它们会有所不同,jQuery不能完全支持它们并选择抽象它们的另一个原因:

http://msdn.microsoft.com/library/ie/ms534361.aspx http://msdn.microsoft.com/library/ie/ms534361.aspx

https://developer.mozilla.org/en-US/docs/Web/API/document.readyState https://developer.mozilla.org/zh-CN/docs/Web/API/document.readyState

(couldn't find an authoritative source for chrome) (找不到chrome的权威来源)

Actually sounds like you want the function to fire upon the start of the AJAX request. 实际上听起来像您希望函数在AJAX请求启动时触发。

$("#btn-ajax").ajaxStart(myFunc1);

function myFunc1() {
   $("#message").html("I don't really care if the ajax call succeeds.");
}

This is what i did to run my callback function sufficiently after the AJAX call had been made so the browser doesn't cancel it and without having to wait for the server to complete the resquest: 这是我在执行AJAX调用后充分运行我的回调函数的目的,因此浏览器不会取消它,而不必等待服务器完成请求:

  var cb = function(){
    window.document.location.href="http://google.com";
  }

  $.ajax({
    url: 'x.html',
    complete: cb,
    timeout: 100
  });

Here the timeout makes sure that if my server doesn't repond in 100ms , the callback is executed. 这里的超时可确保如果我的服务器在100ms内未响应,则执行callback 100ms also is sufficient time for the browser to send the request. 浏览器发送请求也需要100ms时间。

I experimented with status codes and finally ended up not using them. 我尝试了状态码,最后最终没有使用它们。 I found them to unreliable cross browser and for JSONP requests missing. 我发现它们无法跨浏览器运行,并且缺少JSONP请求。

If you really don't care if the ajax call succeeds, you could try using the settimeout method and just kinda estimate how long the request should take. 如果您真的不在乎ajax调用是否成功,则可以尝试使用settimeout方法,只是估算请求应花费的时间。 You'd also have a completion method in case it actually finishes before your estimate. 您还可以采用一种完成方法,以防实际完成之前超出您的估计。 It's kind of a hack, but it may work for you. 这是一种破解,但可能对您有用。 Eg code would be something like this: 例如,代码如下所示:

$.ajax({url: 'x.html', complete: myFunc1});
setTimeout(myFunc1,100);

I think you want to use the beforeSend option. 我认为您想使用beforeSend选项。

see http://api.jquery.com/jQuery.ajax/ 参见http://api.jquery.com/jQuery.ajax/

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

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