简体   繁体   English

如何执行顺序的ajax调用?

[英]How do I perform sequential ajax calls?

I have a button button1 and once it is clicked it redirects to a url www.first.com . 我有一个按钮button1 ,一旦单击它就会重定向到URL www.first.com This url contains another url in its response www.second.com . 该URL在其响应www.second.com包含另一个URL。 I intercept the request with ajax call and instead of redirecting to www.first.com I redirect to www.second.com . 我使用ajax调用拦截请求,而不是重定向到www.first.com ,而是重定向到www.second.com The following code works perfectly fine, and does what I expect it to do: 以下代码可以正常工作,并且可以完成我期望的工作:

document.getElementById("button1").addEventListener("click", function (e) {
  e.preventDefault();
  $.ajax({
    url: 'http://www.first.com',
    method: 'GET',
    crossDomain : true,
    success: function(response){
      //here I am redirected to www.second.com
      window.location.href = response;
     }
  });
});

I want to do another ajax request - the page www.second.com authomaticaly gets redirected to www.third.com . 我要执行另一个Ajax请求-将www.second.com authomaticaly www.second.com重定向到www.third.com I dont want to do some simple string manipulation on the url (lets say I want to change it to www.third2.com ) and then to redirect directly to www.third2.com . 我不想对url做一些简单的字符串操作(假设我想将其更改为www.third2.com ),然后直接重定向到www.third2.com How do I do that? 我怎么做? I guess I need another ajax call but I dont know how to make it run sequentially after finishing the first one. 我猜我需要另一个ajax调用,但是我不知道如何在完成第一个ajax之后按顺序运行。

Posting what I have tried so far and what didnt work: 发布到目前为止我已尝试过的内容以及未成功的内容:

success: function(response){
     $.ajax({
        url: response,
        method: 'GET',
        crossDomain : true,
        xhrFields: {
          withCredentials: false
        },
        success: function(result2){
          //it does not redirect to `google.com`; it still redirects to `www.second.com`
          result2 = "https://www.google.com/";
          window.location.href = result2;
          alert("This alert does not show");
        }
      });
      //maybe I have to remove this line? 
      window.location.href = response;
     },

You can simply make another ajax request inside your first request callback, like this: 您可以在第一个请求回调中简单地发出另一个ajax请求,如下所示:

document.getElementById("button1").addEventListener("click", function (e) {
  e.preventDefault();
  $.ajax({
    url: 'http://www.first.com',
    method: 'GET',
    crossDomain : true,
    xhrFields: {
      withCredentials: false
    },
    success: function(response){
      $.ajax({
        url: response,
        method: 'GET',
        crossDomain: true,
        xhrFields: {
          withCredentials: false
        }
      },
      success: function(response2){
        window.location.href = response2;
      });
    }
  });
});

It's not pretty though, but you can get cleaner code using promises for instance. 虽然不是很漂亮,但是您可以使用例如promises获得更干净的代码。

To clarify, you aren't intercepting a redirect. 需要说明的是,您没有拦截重定向。 You are making an outbound request on a button click. 单击按钮即可发出出站请求。 I only point this out so that we are speaking the same language. 我只是指出这一点,以便我们说相同的语言。 Because, once you do get your secondary URL, you ARE redirecting to that location. 因为,一旦获得辅助URL,就将重定向到该位置。

So, your answer is dependent on what you're really trying to accomplish. 因此,您的答案取决于您实际要完成的工作。 If you simply want to take the returned URL and make another AJAX request to it (and either manipulate it, redirect to it, or both), just nest the secondary request within your success function. 如果您只是想使用返回的URL并对其发出另一个AJAX请求(并对其进行操作,重定向到它,或同时进行这两者),则只需将辅助请求嵌套在您的success函数中即可。

However, if you want to redirect to this second location AND THEN make a subsequent request, this is more complicated. 但是,如果您想重定向到第二个位置,然后进行后续请求,则更为复杂。 You will have to make sure that the page represented by the second URL has an on-load method that makes either another AJAX request or a page-redirect, whichever you are trying to accomplish. 您必须确保由第二个URL表示的页面具有一个加载方法,该方法可以发出另一个AJAX请求或页面重定向,无论您要完成什么。 Once you set your window.location.href (after getting that second URL), the browser is going to attempt to redirect. 设置完window.location.href后(在获得第二个URL之后),浏览器将尝试重定向。 Any on-page-unload events are going to be unreliable, browser-to-browser (in my experience). 任何页面上的卸载事件都将是不可靠的,从浏览器到浏览器(以我的经验)。

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

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