根据fetch规格,只要指定了Content-Type
,即" application/x-www-form-urlencoded"," multipart/form-data",或满足"文本/普通"和其他条件,然后发布后请求不应导致前飞行前请求。但是,实际上,我很难以一种不会导致选项请求前飞行检查的方式指定多个标头。
ex 1.
fetch("https://differentsubodmain.example.com/api/resource", {
headers: {
"Content-Type": "text/plain, application/json",
Accept: "application/json"
},
method: "POST",
body: JSON.stringify({})
})
ex 2.
var myHeaders = new Headers();
myHeaders.append('Accept', 'application/json');
myHeaders.append('Content-Type', 'text/plain');
myHeaders.append('Content-Type', 'application/json');
fetch("https://differentsubodmain.example.com/api/resource", {
headers: myHeaders,
method: "POST",
body: JSON.stringify({})
})
ex 3。
fetch("https://differentsubodmain.example.com/api/resource", {
headers: [
["Content-Type", "application/json"],
["Content-Type", "text/plain"],
["Accept", "application/json"]
],
method: "POST",
body: JSON.stringify({})
})
这些示例均未成功地请求,没有前飞行请求,但仅使用"Content-Type": "text/plain"
指定的是工作正常。但是,这里的示例显示了两者都在请求中指定,并建议它不应引起前飞行。这只是不同浏览器实现的问题还是我缺少某些内容?
看来我没有仔细阅读该参考文献。以下是重要的摘录。
警告。这有意不使用提取MIME类型,因为该算法是宽容的,并且无法实施服务器。
如果使用了提取物类型,则以下请求不会导致CORS前跨前任,并且服务器上的幼稚解析器可能会将请求主体视为JSON
看来我们在很大程度上被限制在MIME类型application/x-www-form-urlencoded
,multipart/form-data
或text/plain
上,以避免对CORS的前推式请求。
参考:
- https://fetch.spec.whatwg.org/#example-cors-safelisted-request-header-content-type