我已遵循JSONPlaceholder文档中提到的有关发送POST请求的确切代码
0: {title: "test", artist: "test", id: 3}
我的代码-(我还没有包含我认为不是错误原因的标头 P.S-即使我包含标题,问题仍然存在。)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))
// Output
{
id: 101,
title: 'foo',
body: 'bar',
userId: 1
}
错误-
可以从“ https://jsonplaceholder.typicode.com/posts”处获取 原点“ null”已被CORS政策阻止:否 请求中出现“ Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。 POST https://jsonplaceholder.typicode.com/posts网:: ERR_FAILED