小编pin*_*ric的帖子

获取API和Cordova

我在使用Cordova和fetch API时遇到问题.我正在执行以下代码

fetch(BASE_URL + '/auth/login', {
  method: 'post',
  credentials: 'include',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: transformRequest({username: email, password: password})
}).then(response => {
      console.log(response.headers.get('X-AuthToken'))
});
Run Code Online (Sandbox Code Playgroud)

在浏览器中执行代码时,会正确检索并记录"X-AuthToken"标头.当我在Cordova应用程序中打包时运行相同的代码时,'X-AuthToken'标头为空.此外,奇怪的是,我可以在检查响应服务器端时以及在网络上嗅探时完全看到标头集,因此我完全确定标头在那里(只是它不会被fetch API返回); 事实上,当使用等效的XMLHttpRqeuest时,标头被正确设置:

var xhttp = new XMLHttpRequest();
xhttp.open("POST", BASE_URL + /api/auth/login", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("username=username&password=password");
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       console.log (xhttp.getResponseHeader('X-AuthToken'));
    }
}
Run Code Online (Sandbox Code Playgroud)

值得注意的是,当我尝试转储其他常见标头(如pragma,cache-control等)时,它们被正确记录.它接口就像fetch API正在过滤标题并删除那些不标准的标题.是否有其他人遇到同样的问题?我错过了什么吗?

javascript http-headers cordova

8
推荐指数
1
解决办法
1186
查看次数

标签 统计

cordova ×1

http-headers ×1

javascript ×1