如何使Ajax请求的异步?Ajax

2023-09-11 01:24:15 作者:@睫毛溺水了

我希望做一些Ajax请求的异步。  诸如两个Ajax中,第一Ajax的不需要完成,第二请求可以发生,

I want to make some ajax requests asynchronous . such as two Ajax, The first Ajax doesn't need to completed, the second request can take place,

$.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"
});

在urlone背景下,我使线程睡眠5秒, 而在urltwo背景我返回的JSON马上。

in the urlone background,I make the thread sleep 5 seconds, and in the urltwo background I return the json Immediately.

我写这篇文章,但没有效果, 我希望控制台打印 2 3 1

I write this, but no effect, I want the console print 2 3 1

但是,实际上,它的打印 2 1 3

but,actually,it print 2 1 3

本次车展的第二个请求只能在第一个请求返回后触发时

this show the second request can be trigged only after the first request return

如何解决这个问题?

在新增2月6日:感谢你的热情的回答。 我的服务器端是C# 而在urlone 我写了

Added in February 6th:Thanks for your enthusiasm answer. My server side is C# and in urlone I wrote

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

在urltwo 我写了

in urltwo I wrote

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

这就是它!

我想这是因为我在urlone进行非常耗时的操作, 当我从urlone回来,前面已经失去响应,即使我设置了一个非常大的ajax等time.So我想查询在urlone的调度研究是否按要求urltwo完成后,urltwo是专为查询数据库的完整场标志对于在urlone操作。 这是我的想法。

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. This is my idea.

但今天,我发现了一个新的问题。 我睡觉的时候后台线程很长一段时间,然后再返回JSON数据前,前端可响应,并做了一些js函数,但是当我在urlone后台运行一个非常耗时的操作,当它返回到前面,前面是无反应。 必须有一些问题,我还没有意识到, 所以

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. there must be some problem I have not realized, so

推荐答案

您已经说你想的的请求的异步的;他们是(你不需要异步:真正的,这是默认的)。

You've said you want to make the requests asynchronous; they are (and you don't need async: true, that's the default).

本次车展的第二个请求只能在第一个请求返回后触发时

this show the second request can be trigged only after the first request return

没有,这表明第一个完成前一秒,而不是第二次是不会触发,直到第一个完成。

No, it shows that the first one completed before the second, not that the second wasn't triggered until the first one completed.

如果你确实看到 2 1 3 的反复试验,告诉你两件事之一:

If you're reliably seeing 2 1 3 in repeated tests, that tells you one of two things:

第一个要求是天生更快地处理比第二 —但如果你拿着的 urlone 的处理五秒钟(和你的验证的那个),那么它的不是这个。

The first request is inherently faster to process than the second— but if you're holding up the processing of urlone for five seconds (and you've verified that), then it's not this

服务器序列化到

服务器可能是序列化他们任何一种理由;什么这些原因都依赖于服务器端技术和配置您使用。例如,部分的服务器将序列化是同一个会话的请求动态生成的内容。

The server may be serializing them for any of several reasons; what those reasons are depends on the server-side technology and configuration you're using. For instance, some servers will serialize requests for dynamically-generated content that are in the same "session".