I have a web page with several jQuery ajax calls which fire asynchronously at the same time against .NET web methods.
These web methods return data back to be processed by the success callbacks for each .ajax call.
My question is probably low level. How does the jQuery/Javascript know which data goes back to which .ajax call? I looked at the return packet using a network sniffer and I couldn’t see any type of identification which could be used to link it back to its originating call. My guess it has to do with the jqXHR object. If it’s in the response packet, I can’t see it in the sniffer.
Any technical explanation could be helpful.
Addition:
I am asking this because I will use asynchronous methods in the server side. The method the .ajax originally called is not the one returning the data. A different thread is doing the work.
The same way a browser can request multiple images from a web server and know where to put each one on the page. It’s part of the HTTP protocol. The browser sends a request, and the response gets sent on the same TCP socket. If the browser is waiting for multiple objects simultaneously, it has multiple TCP sockets open.
In your packet sniffer, look closely at the TCP port numbers, particularly the response port (the port which is not 80).
Your browser and your web server / framework will do the right thing. On the server side make sure you send your response to the appropriate request, and it will get back to the right place in jquery regardless of order received / how long processing took. If you’ve got multiple threads exchanging data on the server, you’re making your life harder than it needs to be, so I assume you have a good reason to and know what you’re doing.