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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:58:34  来源:igfitidea点击:

Http-Method changes from POST to OPTIONS when changing Content-Type

javascriptxmlhttprequestgoogle-closureplovr

提问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 OPTIONSmethod that you are seeing. There is not a way to circumvent this if you are adding custom headers, unfortunately. The POSTwill be sent after the OPTIONS.

当您向 XHR 对象添加标头时,大多数浏览器都会执行预检请求,这就是OPTIONS您所看到的方法。不幸的是,如果您要添加自定义标头,则没有办法规避这一点。在POST会后发出OPTIONS

This articleexplains the OPTIONSrequest a bit. I raninto issues with the preflight a while back, if that is any help.

这篇文章OPTIONS稍微解释了这个请求。不久前,我遇到了预检问题,如果这有帮助的话。

If you have specific issues with the OPTIONSrequest 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-Typeon authorizationrequest it will convert POSTmethod to OPTIONSmethod so while we are use ouathand passing authorization tokenthat time do not required Content-Type.

如果您Content-Typeauthorization请求传递,它会将POST方法转换为OPTIONS方法,因此当我们使用ouath并传递授权令牌时,不需要时间Content-Type

So do not passContent-Typeon all authorizationrequest it won't change your method POSTto OPTIONS

所以没有通过Content-Type所有authorization要求它不会你的方法改变POSTOPTIONS