在提出这样的请求时:
return fetch(
'http://localhost:8000/login',
{ method: 'POST',
headers: new Headers(
{"Content-Type": "application/json",
"Accept":"application/json"}
),
body: JSON.stringify(
{'name': 'Tom', 'password': 'Soyer'}
)
}
).then( response => { console.log(response);})
.catch(err => console.log(err))
请求使用方法 OPTIONS 而不是 POST 运行。
仅在添加模式下:“no-cors”请求变为 POST:
return fetch(
'http://localhost:8000/login',
{ method: 'POST',
mode: 'no-cors',
headers: new Headers(
{"Content-Type": "application/json",
"Accept":"application/json"}
),
body: JSON.stringify(
{'name': 'Tom', 'password': 'Soyer'}
)
}
).then( response => { console.log(response);})
.catch(err => console.log(err))
但响应不正常(即使网络响应状态为 200):{type: "opaque", url: "", status: 0, ok: false, statusText: ""...}
我想是因为
Content-Type 标头唯一允许的值为:
应用程序/x-www-form-urlencoded multipart/form-data text/plain
此处描述https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
有什么方法可以通过 fetch 实时获取 POST json 数据吗?