Search code examples
angularjscorsangular-resource

Request changed from POST to OPTIONS hangs


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


Solution

  • 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