Request changed from POST to OPTIONS hangs

别等时光非礼了梦想. 提交于 2019-12-08 06:42:30

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/28305642/request-changed-from-post-to-options-hangs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!