Http method changes from POST to OPTIONS when Content-Type changes

I use a closure library to do a simple POST. I think XhrIo should work, because from my machine, when I use any other client for relaxation, for example, the Firefox RESTClient application or the Chrome Simple Rest Client, I can make a POST request to the server, and the content type is application / json .

But from my application I can not make a message. I am using the following code

xhr = new goog.net.XhrIo; xhr.send('http://myhost:8181/customer/add','POST', goog.json.serialize(data)); 

If I leave the default headers, I get this

 Encoding: UTF-8 Http-Method: POST Content-Type: application/x-www-form-urlencoded;charset=UTF-8 

If I try to change the title by passing {'content-type':'application/json'} as the 4th parameter, the title will change to

Http-Method: OPTIONS
Content-Type:

Shouldn't I change headers appropriately with the Closure library, like RESTClient does with XMLHttpRequest using jQuery?

How else can I change the title to look like this:

 Encoding: UTF-8 Http-Method: POST Content-Type: application/json;charset=UTF-8 

Appreciate any help Eddie

+4
source share
3 answers

When you add a title to an XHR object, most browsers will do a pre-sale request, which is the OPTIONS method you see. Unfortunately, there is no way around this if you are adding custom headers. POST will be sent after OPTIONS .

This article explains the OPTIONS request. I ran into problems with pre-flight time if this is any help.

If you have special problems with the OPTIONS request, you should change your question to include them; otherwise this is the expected behavior.

+8
source

FWIW mine was also unable to update the type when I specified ...

 {'content-type':'application/json'} 

However, if I fixed the case with

 {'content-type':'application/json'} 

... it worked.

Show drawing.

+1
source

If you pass a Content-Type on authorization request, it will convert the POST method to the OPTIONS method, so for now we use ouath and pass the authorization token this time is not required Content-Type .

So, do not pass Content-Type in all authorization requests, this will not change your POST method to OPTIONS

0
source

All Articles