Neeraj
Neeraj

Reputation: 4489

How server knows whether the request is Synchronous or Asynchronous?

When i make an ajax call to server the full page is not postback, only few amount of data goes to the server and return a response page.

But i am wondering about processing. How the Server or server code knows whether the request in normal call or Ajax call.

I request to experts, please clear my doubt.

Thanks in advance.

Upvotes: 0

Views: 2361

Answers (3)

Spoike
Spoike

Reputation: 121772

How the Server or server code knows whether the request in Normal call or Ajax call.

The server knows this if your javascript code marks the HTTP packet as such. E.g. in jQuery the HTTP header sent to the server has an X-Requested-With set and ASP.NET uses this to distinguish if HTTP packets are ajax calls or not.

To know more about HTTP packets you can inspect the ones sent either in a packet sniffer such as Fiddler or in a browser with dev. tools that monitors traffic. In the latter case you can see this in e.g. Chrome dev tools by doing the following:

  1. Open up Chrome Developer Tools, Ctrl+Alt+I (or Cmd+Alt+I in Mac).
  2. Select the Network tab (you may have to refresh the page to enable network monitoring)
  3. Perform the Ajax call, the HTTP request made should show up in the list at the bottom.
  4. Select the relevant packet, you should now see "Headers", "Preview", "Response", "Cookies" and "Timing" tabs for the selected packet.
  5. Select the "Headers" tab
  6. You may have to expand the Request Headers part. Among the headers should be X-Requested-With: XMLHttpRequest

Here is a screenshot of the tool looking at packages as I was editing this answer:

Chrome Dev Tools inspecting a request while editing this answer

Note that ajax calls don't necessarily have to be asynchronous as they can be synchronous (blocking the javascript until response is loaded) as well. Synchronous calls are necessary sometimes, e.g. popup blockers don't allow you to open a browser window inside an asynchronous ajax callback.

Upvotes: 4

Quentin
Quentin

Reputation: 943569

How the Server or server code knows whether the request in Normal call or Ajax call

It doesn't. There is nothing about an HTTP request sent by Ajax that is any different from any other HTTP request.

The code that makes the request can do something to make it recognisable (e.g. by adding a query string, by changing the Accept header to something more suitable for the context (such as Accept: application/json) or by adding additional HTTP headers (some libraries add X-Requested-With: XMLHttpRequest).

None of those are guarantees as someone could always make an HTTP request manually. They are fine for determining which view to return within your own application, but not if you are trying to implement any kind of security.

Upvotes: 1

Tommi
Tommi

Reputation: 3247

AJAX calls performs with instance of XmlHttpRequest prototype. 3rd argument of its .open() method is async:bool. So xhr.open("GET", "http://example.com", true) is async and xhr.open("GET", "http://example.com") is sync. jQuery get(), post() and ajax() is async by default and you need to pass async param to make it synchronous. So answer to your question: YOU tell the browser what request you want.

Upvotes: 0

Related Questions