我试图使用Angular传递HTTP标头和请求“ App-Id”和“ App-Key”,以从此API获取数据。
我试过像这样设置标题:
$http.defaults.headers.common["App-Id"] = '5a3d8b8d';
$http.defaults.headers.common["App-Key"] = '738e9aca62e7465446b7be8fe4219ffa';
但我有一个Response for preflight is invalid
错误。
$http
在每个请求的基础上向请求添加标题要$http
在每个请求的基础上向请求添加标头,请将其添加到config对象的headers
属性中$http
。
var xheaders = {};
xheaders["App-Id"] = '5a3d8b8d';
xheaders["App-Key"] = '738e9aca62e7465446b7be8fe4219ffa';
var xurl = 'https://uk.bookingbug.com/api/v1';
var configObj = { method: 'GET',
url: xurl,
headers: xheaders
};
$http(configObj)
.then(function onFulfilled(response) {
console.log(response);
vm.headers = response.config.headers;
vm.data = response.data
}).catch( function onRejection(errorResponse) {
console.log("Error: ", errorResponse.status);
console.log(errorResponse);
vm.error = errorResponse;
})
;
该代码收到飞行前错误,因为它使用了不正确的URL。正确的基本URLhttps://uk.bookingbug.com/api/v1
支持App-Id
标题和CORS。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句