2

I've got some Angular code where I am attempting to send a POST to a dev server.

var url = 'http://someDevUrl.com',
    data = { 'someKey': 'some value' };
$http.post(url, data);

It sends the OPTIONS preflight request. I can see it hit the server, and the server gives it a happy response.

OPTIONS Response Headers:

HTTP/1.1 200 OK
Server: Cowboy
Date: Mon, 08 Dec 2014 18:20:44 GMT
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 30
Access-Control-Allow-Headers: x-requested-with, content-type, accept, origin, authorization, x-csrftoken
Content-Type: text/html; charset=utf-8
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS
Via: 1.1 vegur

but then Angular never sends the POST after that...

Typically when $http.post() only sends an OPTIONS request, that means the OPTIONS request returned an error (usually a CORS issue). But in this case, all is fine with the OPTIONS request & response, but it still won't send the POST.

Has anyone seen this before or have an idea of what might be preventing it from sending the POST?


EDIT:
I've got around the issue by adding a Content-Type: text/plain header to the request:

var url = 'http://someDevUrl.com',
    data = { 'someKey': 'some value' },
    config = {
        headers: {
            'Content-Type': 'text/plain'
        }
   }
};
$http.post(url, data, config);

which causes it to skip the OPTIONS preflight, thus avoiding the issue. I'm still curious to know why it was not working in the first place, since there was/is no CORS issue and the OPTIONS request was not sending back an error response.

Community
  • 1
  • 1
Troy
  • 21,172
  • 20
  • 74
  • 103
  • I'm going to bet that you need to go ahead and at minimum, define your .success(d,s,h,c) callback. See if that doesn't cause the behavior you're looking for. – Brant Dec 08 '14 at 19:54
  • Thanks for the response. I actually do have a success() callback defined in the actual code, but I excluded it from the example code, above, as it didn't seem relevant to the question. The success/then callback isn't used/relevant until after the POST is sent, so it doesn't make a difference when I include/exclude it since it never gets to the point of sending the POST. – Troy Dec 08 '14 at 20:26
  • I ran a test on my end against one of my APIs to verify your response... One line that concerns me is this: Connection:keep-alive // this is what I get back, yours says close... So my next place would be to look and see if you could prevent the API from sending back a close response. – Brant Dec 08 '14 at 20:54
  • See my [answer](http://stackoverflow.com/questions/22968406/how-to-skip-the-options-preflight-request-in-angularjs/36086583#36086583/) on similar question – M.Void Mar 18 '16 at 14:23

0 Answers0