这是我的样本请求:
;
当我调试时,我看到此请求已成功发送到服务器,但服务器未收到标头参数(在这种情况下为var header = new Headers({
'Platform-Version': 1,
'App-Version': 1,
'Platform': 'FrontEnd'
});
var myInit = {
method : 'GET',
headers: header,
mode : 'no-cors',
cache : 'default'
}
fetch('http://localhost:3000/api/front_end/v1/login', myInit)
.then(res => {
console.log(res.text())
})
,Platform-Version
和App-Version
)。请告诉我哪个部分配置错误。
感谢
答案 0 :(得分:5)
您正确使用它,但您必须告诉后端服务允许自定义标头(X-
)。例如,在PHP中:
header("Access-Control-Allow-Headers: X-Requested-With");
此外,您的自定义标头应以X-
为前缀。所以你应该:
'X-Platform-Version': '1'
最后一件事,mode
需要cors
。
您可以看到使用以下代码发送标准标头。查看网络选项卡以查看标准请求标题。
var header = new Headers();
// Your server does not currently allow this one
header.append('X-Platform-Version', 1);
// You will see this one in the log in the network tab
header.append("Content-Type", "text/plain");
var myInit = {
method: 'GET',
headers: header,
mode: 'cors',
cache: 'default'
}
fetch('http://localhost:3000/api/front_end/v1/login', myInit)
.then(res => {
console.log(res.text())
});