javascript 更改 Content-Type 时,Http-Method 从 POST 更改为 OPTIONS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11378075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Http-Method changes from POST to OPTIONS when changing Content-Type
提问by Eddie
I am using closure library to do a simple POST. I think XhrIo should work because from my machine when I use any other rest client ,like Firefox browser app RESTClient or Chrome's Simple Rest Client , I can make POST request to the server and content type is application/json.
我正在使用闭包库来做一个简单的 POST。我认为 XhrIo 应该可以工作,因为在我的机器上,当我使用任何其他 rest 客户端时,例如 Firefox 浏览器应用 RESTClient 或 Chrome 的 Simple Rest Client,我可以向服务器发出 POST 请求,内容类型为 application/json。
But from my application I am unable to make a post. 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 headers default, I get this
如果我保留标题默认值,我会得到这个
Encoding: UTF-8 Http-Method: POST Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Encoding: UTF-8 Http-Method: POST Content-Type: application/x-www-form-urlencoded;charset=UTF-8
If I try to change the header by passing {'content-type':'application/json'}
as 4th parameter, header changes to
如果我尝试通过{'content-type':'application/json'}
作为第 4 个参数传递来更改标题,标题将更改为
Http-Method: OPTIONS
Content-Type:
Http 方法:选项
内容类型:
Shouldn't I be able to change headers appropriately with Closure library just as RESTClient does with XMLHttpRequest using JQuery ?
我不应该能够像 RESTClient 使用 JQuery 使用 XMLHttpRequest 那样使用 Closure 库适当地更改标头吗?
How else can the header be altered to make it appear like this
如何更改标题以使其看起来像这样
Encoding: UTF-8 Http-Method: POST Content-Type: application/json;charset=UTF-8
Encoding: UTF-8 Http-Method: POST Content-Type: application/json;charset=UTF-8
Appreciate any help Eddie
感谢任何帮助埃迪
回答by Achal Dave
When you add a header to an XHR object, most browsers will do a preflight request, which is the OPTIONS
method that you are seeing. There is not a way to circumvent this if you are adding custom headers, unfortunately. The POST
will be sent after the OPTIONS
.
当您向 XHR 对象添加标头时,大多数浏览器都会执行预检请求,这就是OPTIONS
您所看到的方法。不幸的是,如果您要添加自定义标头,则没有办法规避这一点。在POST
会后发出OPTIONS
。
This articleexplains the OPTIONS
request a bit. I raninto issues with the preflight a while back, if that is any help.
这篇文章OPTIONS
稍微解释了这个请求。不久前,我遇到了预检问题,如果这有帮助的话。
If you have specific issues with the OPTIONS
request you should edit your question to include them; otherwise, this is expected behavior.
如果您对OPTIONS
请求有特定问题,您应该编辑您的问题以包含它们;否则,这是预期的行为。
回答by Rory Becker
FWIW mine also failed to update the type when I specified...
当我指定时,FWIW 我的也未能更新类型......
{'content-type':'application/json'}
However, if I corrected the case to
但是,如果我将情况更正为
{'Content-Type':'application/json'}
... it worked.
... 有效。
Go figure.
去搞清楚。
回答by Parth Solanki
If you are pass Content-Type
on authorization
request it will convert POST
method to OPTIONS
method so while we are use ouathand passing authorization tokenthat time do not required Content-Type
.
如果您Content-Type
按authorization
请求传递,它会将POST
方法转换为OPTIONS
方法,因此当我们使用ouath并传递授权令牌时,不需要时间Content-Type
。
So do not passContent-Type
on all authorization
request it won't change your method POST
to OPTIONS
所以没有通过Content-Type
所有authorization
要求它不会你的方法改变POST
到OPTIONS