ASP.NET MVC - 什么是IsAjaxRequest()实际上是什么意思? [英] ASP.NET MVC - What does IsAjaxRequest() actually mean?

查看:161
本文介绍了ASP.NET MVC - 什么是IsAjaxRequest()实际上是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously创建的方法对我的基本控制器:

I had previously created a method on my base controller:

public bool IsJsonRequest()
{
    var acceptTypes = Request.AcceptTypes;
    return acceptTypes != null && 
           acceptTypes.Any(a => a.Equals("application/json",
                                         StringComparison.OrdinalIgnoreCase));
}

然后,我偶然发现了 AjaxRequestExtensions.IsAjaxRequest()。该<一href="http://msdn.microsoft.com/en-us/library/system.web.mvc.ajaxrequestextensions.isajaxrequest%28v=VS.90%29.aspx">documentation因为它是非常模糊:

如果指定的HTTP请求是一个Ajax请求;否则为false。

true if the specified HTTP request is an AJAX request; otherwise, false.

这让我思考,是什么约,它使AJAX HTTP请求?是否有更多的东西比检查申请者的接受的内容类型,或者是编码不被AJAX?要求

This got me thinking, what does it about an HTTP request that makes it AJAX? Is there more to it than checking the requester's accepted content types, or is encoding not a requirement of being 'AJAX'?

(注意,在我的情况下,我的行动只能从我的网​​站上JS网页调用,JSON是只需要编码。)

(Note that in my case my actions should only be called from JS pages on my site, and JSON is the only necessary encoding.)

推荐答案

它检查了 X-要求 - 以(HTTP_X_REQUESTED_WITH)头被设置为XMLHtt prequest。这头的jQuery设置,使AJAX请求时,其他一些JavaScript框架。

It checks for the X-Requested-With (HTTP_X_REQUESTED_WITH) header being set to XMLHttpRequest. This header is set by jQuery and a number of other javascript frameworks when making AJAX requests.

这篇关于ASP.NET MVC - 什么是IsAjaxRequest()实际上是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆