简体   繁体   中英

Can the "success" callback of jQuery.ajax() distinguish two calls to the same script?

Here is the code:

$.ajax({
        url: "script.php",
            success : oneHandler
        });

$.ajax({
        url: "script.php",
            success : twoHandler
        });

When calling these two function calls concurrently is it ensured that "oneHandler" is really called on the response of the first call? In other words, is the appended string checked for match?

What if I add data:

dataType: 'json',
data: {"json": "one"} ,

dataType: 'json',
data: {"json": "two"} ,

respectively?

What if I use the post method?

Of course, JavaScript being a single threaded model in your browser, there's no such thing as truly concurrent; there's only very close together.

Internally, an object is created for each request you make via $.ajax and friends; this object is the one talking to the server and receiving the response. Your success callback is attached to that object.

Because each request is a separate object, it's guaranteed that the correct callback is executed. Note that multiple requests can coexist at the same time, they don't wait for each other to finish; if you want that you could use Deferred.pipe .

When calling these two function calls concurrently is it ensured that "oneHandler" is really called on the response of the first call?

Yes

What if I use the post method?

Doesn't matter. It's guaranteed to call a correct callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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