我正在尝试在 AWS 上执行一个需要 CORS header 的 API,我使用所有信息发出请求,但我收到下一个错误:
“TypeError: headers[key].trim 不是一个函数”
如果我不发送 de CORS,我会收到错误:
来源“http://localhost:3000”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin” header 。
我的代码:
async function Request(){
let arra = "test"
const data2 = {
"parm1" : "one", "parm2" : "two"
};
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
};
try{
const resp = await API.post("Lambda", "/admin/digital/app/uploadtodynamo", {
body: data2, headers: headers
});
console.log("error 1")
console.log(resp)
}
catch(error)
{
console.log("error 2")
console.log(error)
}
}
react 版本:“^16.12.0”
放大:“^2.2.5”
最佳答案
header 值必须是字符串。在发送请求的某个时刻,您(或某些库)正在从尾随和前导空格中 trim header 值。但由于 Access-Control-Allow-Credentials header 的值是 bool 值而不是字符串,因此这是行不通的。
改变
"Access-Control-Allow-Credentials": true
到
"Access-Control-Allow-Credentials": "true"
关于javascript - TypeError : headers[key]. trim 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63923023/