简体   繁体   中英

Postback request and ajax request in Asp.net

Is the any way to find out that a request is a normal request (request such as postback operation or redirect or form submit) or an Ajax request?

I am using JQuery lib to send a request and call a Web Method

Assuming that you are using Web Forms and Update Panels here is what we use:

ScriptManager sm = ScriptManager.GetCurrent(this);
return sm != null && sm.IsInAsyncPostBack;

More general method would be to check for the X-Requested-With header in the request.

Request.Headers["X-Requested-With"] == "XMLHttpRequest"

What is better for you depends on your scenario. Do you want to detect if the page is executing a partial update or you want to know if the browser requested it via XMLHttpRequest?

Another option when using JavaScript to make the AJAX call is to pass a query string parameter indicating that you are making an AJAX call.

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