anybody how can I know if the request is ajax ? (I'm using jquery for ajax)
谁知道请求是ajax吗?(我在ajax中使用jquery)
3 个解决方案
#1
64
All AJAX calls made by jQuery will have a header added to indicate it is AJAX. The header to check is X-Requested-With
, and the value will be XMLHttpRequest
when it is an AJAX call.
jQuery发出的所有AJAX调用都将添加一个头来表明它是AJAX。要检查的头是x - requestewith,当它是AJAX调用时,值将是XMLHttpRequest。
Note that AJAX requests are normal GETs or POSTs, so unless you (or your AJAX library like jQuery) are adding an additional header in the request, there is no way to know for certain whether it is AJAX or not.
注意,AJAX请求是普通的get或post,所以除非您(或您的AJAX库jQuery)在请求中添加了一个额外的头,否则无法确定它是否是AJAX。
#2
161
There's also the Request.IsAjaxRequest
if you're using a later version of MVC. I don't have version 1 anymore so I can't say if it's in version 1.
还有一个请求。IsAjaxRequest如果您使用的是MVC的后期版本。我已经没有版本1了,所以我不能说它是否在版本1中。
If you need this check in Global.asax.cs try this: new HttpRequestWrapper(Request).IsAjaxRequest()
如果你需要在Global.asax登记。cs试试这个:new HttpRequestWrapper(Request).IsAjaxRequest()
#3
48
It works for me in ASP.NET MVC 3
它在ASP中对我很有用。净MVC 3
if (Request.IsAjaxRequest())
{
// ajax request handled
}
#1
64
All AJAX calls made by jQuery will have a header added to indicate it is AJAX. The header to check is X-Requested-With
, and the value will be XMLHttpRequest
when it is an AJAX call.
jQuery发出的所有AJAX调用都将添加一个头来表明它是AJAX。要检查的头是x - requestewith,当它是AJAX调用时,值将是XMLHttpRequest。
Note that AJAX requests are normal GETs or POSTs, so unless you (or your AJAX library like jQuery) are adding an additional header in the request, there is no way to know for certain whether it is AJAX or not.
注意,AJAX请求是普通的get或post,所以除非您(或您的AJAX库jQuery)在请求中添加了一个额外的头,否则无法确定它是否是AJAX。
#2
161
There's also the Request.IsAjaxRequest
if you're using a later version of MVC. I don't have version 1 anymore so I can't say if it's in version 1.
还有一个请求。IsAjaxRequest如果您使用的是MVC的后期版本。我已经没有版本1了,所以我不能说它是否在版本1中。
If you need this check in Global.asax.cs try this: new HttpRequestWrapper(Request).IsAjaxRequest()
如果你需要在Global.asax登记。cs试试这个:new HttpRequestWrapper(Request).IsAjaxRequest()
#3
48
It works for me in ASP.NET MVC 3
它在ASP中对我很有用。净MVC 3
if (Request.IsAjaxRequest())
{
// ajax request handled
}