Javascript XMLHttpRequest/ajax 设置 Content-Type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37664317/
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
XMLHttpRequest/ajax set Content-Type
提问by backcab
I've tried both these things separately:
我已经分别尝试过这两件事:
note: url is a variable containing a https url and jsonString contains a valid json string
注意:url 是一个包含 https url 的变量,jsonString 包含一个有效的 json 字符串
var request = new XMLHttpRequest();
try{
request.open("POST", url);
request.setRequestHeader('Accept', 'application/json');
request.send(jsonString);
} catch(e) {
alert(e);
}
and
和
var options = {
type: "POST",
url: url,
dataType: "json",
data: jsonString,
accept: "application/json"
};
$.ajax(options)
The problem is the system we are posting to requires a header Content-Type with a value "application/json".
问题是我们要发布到的系统需要一个值为“application/json”的标题 Content-Type。
With the way things are right now, the method used is POST, the Accept header is "application/json", and the Content-Type defaults to "application/x-www-form-urlencoded; charset=UTF-8"
按照现在的情况,使用的方法是 POST,Accept 标头是“application/json”,Content-Type 默认为“application/x-www-form-urlencoded; charset=UTF-8”
In the first example, if request.setRequestHeader('Content-Type', 'application/json')
; is added 1 line above or below the Accept header, the method used changes to OPTIONS, the Accept header changes to "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8"and the Content-Typeheader disappears as if it wasn't seen.
在第一个例子中,如果request.setRequestHeader('Content-Type', 'application/json')
;加入以上或Accept首部低于1条线路,所述方法用于修改OPTIONS,Accept首部变为“text / html的,应用/ XHTML + xml的,应用/ XML; Q = 0.9,/Q = 0.8”和Content-Type标头消失了,就好像它没有被看到一样。
In the second example, if contentType: "application/json"is added anywhere within options, the same thing happens that happened in the first example.
在第二个示例中,如果将contentType: "application/json"添加到选项中的任何位置,则会发生与第一个示例中发生的情况相同的事情。
What is the proper way to set a Content-Type header in ajax or XMLHttpRequest?
在 ajax 或 XMLHttpRequest 中设置 Content-Type 标头的正确方法是什么?
Edit:I should add that using the firefox rest client plugin, the json string, url, and accept and content type headers all work fine. We just can't seem to get the content header set on our own page.
编辑:我应该补充一点,使用 firefox rest 客户端插件,json 字符串、url、接受和内容类型标头都可以正常工作。我们似乎无法在我们自己的页面上设置内容标题。
采纳答案by Quentin
In the first example, if request.setRequestHeader('Content-Type', 'application/json'); is added 1 line above or below the Accept header, the method used changes to OPTIONS
在第一个例子中,如果 request.setRequestHeader('Content-Type', 'application/json'); 在 Accept 标头上方或下方添加 1 行,使用的方法更改为 OPTIONS
This is because you are making a cross originrequestfrom JS embedded in a webpage. Changing the content-type
(to one that you couldn't make with a simple HTML form) triggers a preflight requestasking permission from the server to make the actual request.
这是因为你在做一个跨源请求,从JS嵌入网页。更改content-type
(更改为您无法使用简单的 HTML 表单创建的)会触发预检请求,请求服务器发出实际请求的许可。
You need to set up the server to respond with appropriate CORS headers to grant permission.
您需要设置服务器以使用适当的 CORS 标头进行响应以授予权限。
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Access-Control-Allow-Origin: http://your.site.example.com
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: Content-Type
Then the browser will make the POST request you are asking it to make.
然后浏览器将发出您要求它发出的 POST 请求。