[英]POST getting 415 (Unsupported Media Type) while POSTMAN returns 200
Seems like a simple issue but everywhere else suggested adding "application/json" in 'headers' which I have tried.似乎是一个简单的问题,但其他任何地方都建议在我尝试过的“标题”中添加“application/json”。
'Accept'and 'Content-Type' are both "application/json". 'Accept' 和 'Content-Type' 都是“application/json”。 Also tried both json and stringfy 'Body' but keep getting 415 on Chrome extension JavaScript.
还尝试了 json 和 stringfy 'Body',但在 Chrome 扩展 JavaScript 上继续获得 415。
textArraySample = ["sample","sample2"];
var serverUrl = "https://webappcontentnotification.azurewebsites.net/api/ContentUpload";
const body={
"textbatch":textArraySample,
"userId": userId,
"url": window.location.href
}
let result =
fetch(serverUrl, {
method: 'POST',
mode: "no-cors",
headers: {
'Accept': "application/json",
'Content-Type': "application/json"
},
body: JSON.stringify(body)
})
.then(response => {
console.log('response:', response);
})
.catch((error) => {
console.log('Error:', error);
});
Update: seems like "Content-Type" is not correctly set更新:似乎“Content-Type”设置不正确
Did not see this mentioned somewhere else.没有看到其他地方提到过这个。 The root cause is that "Content-Type" cannot be set to "application/json" while using "Mode: no-cors".
根本原因是在使用“Mode: no-cors”时无法将“Content-Type”设置为“application/json”。 link here
链接在这里
mode: "no-cors" only allows a limited set of headers in the request:
mode: "no-cors" 只允许请求中的一组有限的标头:
Accept
接受
Accept-Language
接受语言
Content-Language
内容语言
Content-Type with a value of application/x-www-form-urlencoded, multipart/form-data, or text/plain
Content-Type 值为 application/x-www-form-urlencoded、multipart/form-data 或 text/plain
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.