简体   繁体   English

如何使ajax请求异步,例如两个请求?

[英]how to make ajax request async,such as two requests?

I have post the question in this address how to make ajax request async? 我已经在该地址发布了问题, 如何使ajax请求异步?

,but I have not receiver the good answer. ,但我没有收到好的答案。

and before you answer this question ,you can view the url address above to see the answers that other people give. 在回答这个问题之前,您可以查看上面的网址,以查看其他人给出的答案。

I hope some one can solve my problem. 我希望有人能解决我的问题。

I want to make some ajax requests asynchronous . 我想使一些ajax请求异步。 such as two Ajax, The first Ajax doesn't need to completed, the second request can take place 例如两个Ajax,第一个Ajax不需要完成,第二个请求可以发生

$.ajax({
            url: "urlone",
            async: true, 
              complete: function () {
                console.log("1");
            }, 
            type: "post",
           dataType: "json"
        });


 console.log("2");

 $.ajax({
            url: "urltwo",
            async: true,
              complete: function () {
                console.log("3");
            },
             type: "post", 
             dataType: "json"
});

I want to make some ajax requests asynchronous . 我想使一些ajax请求异步。 such as two Ajax, The first Ajax doesn't need to completed, the second request can take place, 例如两个Ajax,第一个Ajax不需要完成,第二个请求可以发生,

in the urlone background,I make the thread sleep 5 seconds, and in the urltwo background I return the json Immediately. 在urlone背景中,我使线程休眠5秒钟,在urltwo背景中,我立即返回json。

I write this, but no effect, I want the console print 2 3 1 我写了这个,但是没有效果,我想要控制台打印2 3 1

but,actually,it print 2 1 3 但实际上它打印2 1 3

this show the second request can be trigged only after the first request return 此显示仅在第一个请求返回后才能触发第二个请求

How to solve this problem?? 如何解决这个问题呢??

Added in February 6th:Thanks for your enthusiasm answer. 2月6日添加:非常感谢您的热情回答。 My server side is C# and in urlone I wrote 我的服务器端是C#,并在urlone中编写了

JsonModel  xx=new JsonModel(){.......};   //as you known
System.Threading.Thread.Sleep(5000);
return Json(xx);

in urltwo I wrote 我在urltwo中写道

JsonModel  xx=new JsonModel(){.......};   //as you known
return Json(xx);

that's it! 而已!

I want this because I performed a very time-consuming operation in urlone, when I back from urlone,The front has lost response,Even if I set up a very large ajax waiting time。So I want to query whether the opration in urlone is completed by request urltwo,the urltwo is designed for query database complete field flag for the operation in urlone. 我之所以这样,是因为我在urlone中执行了非常耗时的操作,当我从urlone返回时,前端失去了响应,即使我设置了非常长的ajax等待时间。所以我想查询urlone中的操作是否urltwo是由请求urltwo完成的,用于查询urlone中操作的查询数据库完成字段标志。 This is my idea. 这是我的主意。

But today, I found a new problem。 when I sleep background thread a long time,then back json data to front,front can response and done some js function,but when I perform a very time-consuming operation in urlone background,when it return to front,the front is no response. 但是今天,我发现了一个新问题。当我长时间休眠后台线程时,然后将json数据返回到前端,前端可以响应并完成了一些js函数,但是当我在urlone后台执行非常耗时的操作时,回到前面,前面没有反应。 there must be some problem I have not realized, so 肯定有一些我没有意识到的问题,所以

The A in AJAX stands for asynchronous. AJAXA代表异步。 All requests are async by nature. 所有请求本质上都是异步的。

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

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