Angular $ http not sending header in OPTIONS before flight

I'm trying to connect to an API using the $ http service, which requires a specific header for requests POST

:

app.run(['$http', function($http) {
    $http.defaults.headers.post.hello = 'world';
}]);

      

However, my Angular app sends the request OPTIONS

before POST

and it doesn't seem to send the custom header.

Check out the network tab in this Plunker: http://plnkr.co/edit/tcqIb2q9zIQb3nf2SzyT

And look where the request should send a header hello

with a value world

, in fact it is not:

enter image description here

I see hello

in access-control-request-header

, but shouldn't it be on a separate line? And why isn't it sending the value world

?

+3


source to share


1 answer


When you add a header because you are crossing different domains, the requests first send an HTTP request using the OPTIONS method to a resource in the other domain to determine if it is safe to send that request. In this request, the header name is sent in the request-control-request headers to check if it is a safe header to send.

If it's safe to send, the header will be sent using POST.



Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

+5


source







All Articles