Asp.net

檢測ajax呼叫,ASP.net

  • May 8, 2014

如何在 ASP.net 應用程序的伺服器端檢測請求是否是 AJAX 請求(來自 jQuery)。我不想這樣做:mypage.aspx?this_is_ajax=true…

前任。

$.get("mypage.aspx");

在伺服器端,當請求是 ajax 請求時,我想做某些事情……

謝謝!

ASP.NET MVC 對此有一個很好的擴展,它確實適用於 JQuery。它是這樣檢查的:

檢查核心集合:

request["X-Requested-With"] == "XMLHttpRequest"

檢查 headers 集合(確保其不為空):

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

它是作為擴展方法實現的,因此您可以重新創建它,或者如果您使用 ASP.NET MVC 通過Request.IsAjaxRequest().

引用自:https://stackoverflow.com/questions/3441735