fetch 不会让我授权 get 请求
fetch won't let me authorize get request
我正在尝试通过浏览器使用 fetch,但它不起作用。
我试过了:
fetch('https://api-2sizcg3ipa-uc.a.run.app/fats', {
headers: {'Authorization': 'Basic ' + btoa('username:password')},
mode:'no-cors'
})
.then(response => response.json())
.then(json => console.log(json));
但是它给了我两个错误:
GET https://api-2sizcg3ipa-uc.a.run.app/fats net::ERR_ABORTED 403
Uncaught (in promise) SyntaxError: Unexpected end of input
at fetch.js:5
我做错了什么?当我使用 curl 时,我会工作。
你说:
mode:'no-cors'
这使得 fetch
安静地忽略任何需要通过 CORS 许可的内容,而不是因错误而失败。
发送授权headers需要通过 CORS 获得许可。
所以不要说你不想用它。
但是,如果您想发送凭据,请说 credentials: 'include'
。
我正在尝试通过浏览器使用 fetch,但它不起作用。
我试过了:
fetch('https://api-2sizcg3ipa-uc.a.run.app/fats', {
headers: {'Authorization': 'Basic ' + btoa('username:password')},
mode:'no-cors'
})
.then(response => response.json())
.then(json => console.log(json));
但是它给了我两个错误:
GET https://api-2sizcg3ipa-uc.a.run.app/fats net::ERR_ABORTED 403
Uncaught (in promise) SyntaxError: Unexpected end of input at fetch.js:5
我做错了什么?当我使用 curl 时,我会工作。
你说:
mode:'no-cors'
这使得 fetch
安静地忽略任何需要通过 CORS 许可的内容,而不是因错误而失败。
发送授权headers需要通过 CORS 获得许可。
所以不要说你不想用它。
但是,如果您想发送凭据,请说 credentials: 'include'
。