Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

.net - ASP.NET MVC - What does IsAjaxRequest() actually mean?

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));
}

Then I stumbled upon AjaxRequestExtensions.IsAjaxRequest(). The documentation for it is very vague:

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

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'?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...