反应获取 header 未设置
react fetch header not set
我遇到了问题。其他人接受的解决方案对我不起作用。/
我有:
return await fetch(url, {
method: httpMethod,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-test':'try me',
},
body: JSON.stringify(bodyObject)
});
但是,好像根本没有设置header。
Cors-header已设置,添加mode:'cors'
没有区别。
我是不是漏掉了什么?
请求:
Accept
是空的,Content-Type
和 x-test
都找不到。
..
编辑
..
如果我需要创建新问题,请告诉我。
新请求
所以我看到 header 已设置 - 感谢 liminal18!-。但是,现在我想授权给一个AD。
所以现在我得到了:
async callUrl(url, httpMethod, bodyObject) {
try {
var requestObj = {
method: httpMethod,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-test':'try me',
'Authorization': 'Basic Uk11bGxlc....'
}
};
if (bodyObject !== undefined){
requestObj.body = JSON.stringify(bodyObject);
}
return await fetch(url, requestObj);
} catch(error){
console.error(error);
}
}
但仍然得到 401。
我知道这不是登录的干净解决方案,只是测试..
您确定您捕获的请求是正确的请求吗?
如果您使用的是 CORS,则有一个选项 post 可以在反应之前获取 cors 允许的方法 post/put 这可能是您正在查看的内容,而不是实际的 post 或您想要的. CORS headers 设置在哪里?
我遇到了问题。其他人接受的解决方案对我不起作用。/
我有:
return await fetch(url, {
method: httpMethod,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-test':'try me',
},
body: JSON.stringify(bodyObject)
});
但是,好像根本没有设置header。
Cors-header已设置,添加mode:'cors'
没有区别。
我是不是漏掉了什么?
请求:
Accept
是空的,Content-Type
和 x-test
都找不到。
..
编辑
..
如果我需要创建新问题,请告诉我。
新请求
所以我看到 header 已设置 - 感谢 liminal18!-。但是,现在我想授权给一个AD。
所以现在我得到了:
async callUrl(url, httpMethod, bodyObject) {
try {
var requestObj = {
method: httpMethod,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'x-test':'try me',
'Authorization': 'Basic Uk11bGxlc....'
}
};
if (bodyObject !== undefined){
requestObj.body = JSON.stringify(bodyObject);
}
return await fetch(url, requestObj);
} catch(error){
console.error(error);
}
}
但仍然得到 401。 我知道这不是登录的干净解决方案,只是测试..
您确定您捕获的请求是正确的请求吗? 如果您使用的是 CORS,则有一个选项 post 可以在反应之前获取 cors 允许的方法 post/put 这可能是您正在查看的内容,而不是实际的 post 或您想要的. CORS headers 设置在哪里?