Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Control Request Headers, is added to header in AJAX request with jQuery

I would like to add a custom header to an AJAX POST request from jQuery.

I have tried this:

$.ajax({     type: 'POST',     url: url,     headers: {         "My-First-Header":"first value",         "My-Second-Header":"second value"     }     //OR     //beforeSend: function(xhr) {      //  xhr.setRequestHeader("My-First-Header", "first value");      //  xhr.setRequestHeader("My-Second-Header", "second value");      //} }).done(function(data) {      alert(data); }); 

When I send this request and I watch with FireBug, I see this header:

OPTIONS xxxx/yyyy HTTP/1.1
Host: 127.0.0.1:6666
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null
Access-Control-Request-Method: POST
Access-Control-Request-Headers: my-first-header,my-second-header
Pragma: no-cache
Cache-Control: no-cache

Why do my custom headers go to Access-Control-Request-Headers:

Access-Control-Request-Headers: my-first-header,my-second-header

I was expecting a header values like this:

My-First-Header: first value
My-Second-Header: second value

Is it possible?

like image 643
fingerup Avatar asked Apr 10 '12 16:04

fingerup


People also ask

What is request header in ajax?

The headers are additional key-value pairs send along with ajax request using the XMLHttpRequest object. An asynchronous HTTP request to the server by using The ajax() function and by including the header it describes to the server what kind of response it accept.

What is Access Control allow headers?

The Access-Control-Allow-Headers response header is used in response to a preflight request which includes the Access-Control-Request-Headers to indicate which HTTP headers can be used during the actual request. This header is required if the request has an Access-Control-Request-Headers header.


1 Answers

Here is an example how to set a request header in a jQuery Ajax call:

$.ajax({   type: "POST",   beforeSend: function(request) {     request.setRequestHeader("Authority", authorizationToken);   },   url: "entities",   data: "json=" + escape(JSON.stringify(createRequestObject)),   processData: false,   success: function(msg) {     $("#results").append("The result =" + StringifyPretty(msg));   } }); 
like image 183
milkovsky Avatar answered Oct 11 '22 12:10

milkovsky