我正在尝试使用react + axios调用vuforia的网络服务,阅读docs of vuforia并按照这些步骤进行操作,但在chrome的控制台日志中出现错误:
xhr.js:121 Refused to set unsafe header "Date"
但是,如果我理解正确,则必须在请求中声明标头“ Date”。我该如何解决,这是我的代码:
class App extends Component {
componentDidMount() {
var md5 = require('md5');
var base64 = require('base-64');
var hmacsha1 = require('hmacsha1');
var contentType = "application/json";
var hexDigest = "d41d8cd98f00b204e9800998ecf8427e";
var accessKey = "xxxxxxxxxxxx";
var secretKey = "xxxxxxxxxxx";
var date = new Date().toUTCString();
var url = `${'https://cors-anywhere.herokuapp.com/'}https://vws.vuforia.com/targets`;
var dateValue = date;
var requestPath = url;
var newLine = '\n';
var toDigest = `GET${newLine}${hexDigest}${newLine}${contentType}${newLine}${dateValue}${newLine}${requestPath}`;
var shaHashed = hmacsha1(secretKey, toDigest);
var signature = base64.encode(shaHashed);
const config = {
headers: {
'Date': `${date}`,
'Authorization': `VWS ${accessKey}:${signature}`
}
}
console.log(toDigest);
axios.get(url, config,{ crossdomain: true })
.then(json => console.log(json))
}
console.log(toDigest):
GET
d41d8cd98f00b204e9800998ecf8427e
application/json
Mon, 29 Oct 2018 12:45:26 GMT
https://cors-anywhere.herokuapp.com/https://vws.vuforia.com/targets
答案 0 :(得分:1)
从以下位置更改您的配置代码
const config = {
headers: {
'Date': `${date}`,
'Authorization': `VWS ${accessKey}:${signature}`
}
到
const config = {
headers: {
'Authorization': `VWS ${accessKey}:${signature}`
}
XMLHttpRequest不允许设置Date标头,它是由浏览器自动设置的。原因是通过操纵这些标头,您可能能够欺骗服务器通过同一连接接受第二个请求,而该请求不会通过通常的安全检查-这将是浏览器中的一个安全漏洞。这是the list of HTTP headers,您无法自己设置。
让我知道您是否仍然遇到错误。