Request changed from POST to OPTIONS hangs
Asked Answered
I

1

1

I am trying to send a POST request to an endpoint over HTTPS. The request has 2 headers, content-type (application/json) and an apiKey.

I am using the request in a PhoneGap application built in Angular, and when the request is sent its method is changed to OPTIONS.

I know this is standard practice for browsers due to CORS, but I have a payload which I need the server to take, and I'm told by the server guys that OPTIONS requests have an empty payload with CORS (although I can't find verification on this).

The server is set up for CORS and should accept POST and OPTIONS.

For some reason my request hangs.

Angular code:

var submitDBIDResource = $resource(env.loginUserUrl, {}, {
    save: {
        method: 'POST',
        headers: { 'apiKey': apiKey }
     }
  });

submitDBIDResource.save({"dbid": dbid}).$promise.then(function(data) {
       console.log(data);
       return data;
   });

I have in my config.xml file

Any ideas what I need to do?

Thanks

Ion answered 3/2, 2015 at 18:0 Comment(0)
W
0

The browser will automatically send an OPTIONS request before it sends the POST request. The OPTIONS request must respond with the appropriate response or else the browser will not send the POST request.

Your backend guys need to create two request handlers, one for the OPTIONS and one for the POST.

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

Wingard answered 3/2, 2015 at 18:8 Comment(2)
Thanks I'll try this when they are back in tomorrow, will let you knowIon
The suspense is killing me here, did it help? :-DWolfort

© 2022 - 2024 — McMap. All rights reserved.