我试图通过使用 Fetch API 调用我自己的 API 来保存我的数据。但后来的结果是不断回来415 Unsupported Media Type。
客户端使用 React JS 和 .NET Core MVC。服务器端使用托管在 Windows Server 2012 上的 .NET Core Web API。
我已经尝试了网络中提供的所有解决方案,但仍然出现 415 错误。在 IIS 方面,我已经添加Content-Type并Accept接受application/json和text/plain。
到目前为止,只有 GET 方法有效。PUT、POST、DELETE 都不起作用。
下面是我在客户端的代码。
fetch('https://mywebapi.net/api/event/', {
method: 'POST',
headers: {
'Accept': 'application/json, text/plain',
'Content-Type': 'application/json;charset=UTF-8'
},
mode: 'no-cors',
body: JSON.stringify({
eventId: 5,
eventName: "Event 5",
eventDescription: "Event 5 Description",
eventLocation: "Kuala Lumpur, Malaysia",
eventDateTime: "2019-03-28"
}),
}).then(response => response.text())
.then(data …Run Code Online (Sandbox Code Playgroud)