繁体   English   中英

POST 得到 415(不支持的媒体类型)而 POSTMAN 返回 200

[英]POST getting 415 (Unsupported Media Type) while POSTMAN returns 200

似乎是一个简单的问题,但其他任何地方都建议在我尝试过的“标题”中添加“application/json”。

'Accept' 和 'Content-Type' 都是“application/json”。 还尝试了 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);
});

更新:似乎“Content-Type”设置不正确在此处输入图像描述

没有看到其他地方提到过这个。 根本原因是在使用“Mode: no-cors”时无法将“Content-Type”设置为“application/json”。 链接在这里

mode: "no-cors" 只允许请求中的一组有限的标头:

接受

接受语言

内容语言

Content-Type 值为 application/x-www-form-urlencoded、multipart/form-data 或 text/plain

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM