更改 Content-Type 时,Http-Method 从 POST 更改为 OPTIONS [英] Http-Method changes from POST to OPTIONS when changing Content-Type

查看:85
本文介绍了更改 Content-Type 时,Http-Method 从 POST 更改为 OPTIONS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用闭包库来做一个简单的 POST.我认为 XhrIo 应该可以工作,因为在我的机器上,当我使用任何其他 rest 客户端时,例如 Firefox 浏览器应用 RESTClient 或 Chrome 的 Simple Rest Client,我可以向服务器发出 POST 请求,内容类型为 application/json.

但是从我的申请中我无法发帖.我正在使用以下代码

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

如果我保留标题默认值,我会得到这个

<块引用>

编码:UTF-8Http 方法:POST内容类型:application/x-www-form-urlencoded;charset=UTF-8

如果我尝试通过将 {'content-type':'application/json'} 作为第 4 个参数来更改标题,标题将更改为

<块引用>

Http-方法:选项
内容类型:

难道我不能像 RESTClient 使用 JQuery 对 XMLHttpRequest 那样使用 Closure 库适当地更改标头吗?

如何更改标题以使其看起来像这样

<块引用>

编码:UTF-8Http 方法:POST内容类型:application/json;charset=UTF-8

感谢任何帮助埃迪

解决方案

当您向 XHR 对象添加标头时,大多数浏览器都会执行预检请求,这就是您使用的 OPTIONS 方法看到.不幸的是,如果您要添加自定义标头,则无法避免这种情况.POST 将在 OPTIONS 之后发送.

这篇文章 稍微解释了 OPTIONS 请求.我遇到预检问题,如果这是任何帮助.

如果您对 OPTIONS 请求有特定问题,您应该编辑您的问题以包含它们;否则,这是预期行为.

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.

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

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

Http-Method: OPTIONS
Content-Type:

Shouldn't I be able to change headers appropriately with Closure library just as RESTClient does with XMLHttpRequest using JQuery ?

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

Appreciate any help Eddie

解决方案

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.

This article explains the OPTIONS request a bit. I ran into issues with the preflight a while back, if that is any help.

If you have specific issues with the OPTIONS request you should edit your question to include them; otherwise, this is expected behavior.

这篇关于更改 Content-Type 时,Http-Method 从 POST 更改为 OPTIONS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆