簡體   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