var response = await fetch('http://localhost:8080/api/add', {
method: 'POST',
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/x-www-form-urlencoded"
},
body: `link=${encodeURIComponent("https://long-link.net/with-various-parameters?param=1¶m2=3")}`
});
console.log(response.status) // undefined for some reason
一个疯狂的猜测:
如果您将浏览器控制台配置为仅显示错误消息,然后在控制台中逐个输入以下内容:
> var response = await fetch('https://httpbin.org/headers', {
method: 'POST',
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/x-www-form-urlencoded"
},
body: `link=${encodeURIComponent("https://long-link.net/with-various-parameters?param=1¶m2=3")}`
});
undefined
> response
Response {type: 'cors', url: 'https://httpbin.org/headers', redirected: false, status: 405, ok: false, …}
> console.log(response.status)
undefined
您会得到您所描述的响应。但最后一个
undefined
不是 console.log
的输出(因为它被隐藏为用户消息)。是console.log
的返回值。
(请注意 https://httpbin.org/headers 设置 CORS 标头,否则
await fetch
将失败。)
观察到的就是你吗?