JS in Ajax request when set HTTP request header in X-requestd-with= Ajax

Source: Internet
Author: User

Today the $http service that discovers the AngularJS framework provides the $http.get ()/$http. Post () in the AJAX request without the X-requested-with field.

In this case, the backend PHP will not be able to determine whether the accepted HTTP request is an AJAX request.

What to do, obviously it is to add this field to the HTTP request header .

1, AngularJS can be such a child :

I don ' t know well MVC3 and can set a custom header for all request from AngularJS.

Then on the server side you just has to get the this header and does what the want with the request from angular.

To has the custom header in AngularJS just does this:

Angular.module (' MyModule ', [])    . config ([' $httpProvider ', function ($httpProvider) {        $ httpprovider.defaults.headers.common["from-angular"] = "true";    }])

  

The for use of the x-requested-with you has to does this too:

$httpProvider. defaults.headers.common["X-requested-with"] = ' xmlhttprequest ';

It ' s not set by default anymore because a lot part of the community has to delete this header to enable CORS request

2,JQuery Ajax method also provides a way to set the HTTP request header;

  

JS in Ajax request when set HTTP request header in X-requestd-with= Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.