当我使用$http
角度调用我的 API 点时,我不断收到 502 错误。
确切的错误是这样说的:
无法加载https://xxxxxxxxx.execute-api.eu-west-2.amazonaws.com/dev/api/fund:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost:3000 ”。响应具有 HTTP 状态代码 502。
但是,我已经使用 Postman 检查了该标头是否在响应中,并且它是:
Access-Control-Allow-Origin →*
Connection →keep-alive
Content-Length →30
Content-Type →application/json
Date →Tue, 03 Jul 2018 10:01:11 GMT
Via →1.1 xxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)
当我对同一个 URL 进行 OPTIONS 调用时,这里是使用 Postman 的响应的标头:
Access-Control-Allow-Credentials →false
Access-Control-Allow-Headers →Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent
Access-Control-Allow-Methods →OPTIONS,POST
Access-Control-Allow-Origin →*
Connection →keep-alive
Content-Length →0
Content-Type →application/json
Date →Tue, 03 Jul 2018 10:18:17 GMT
Via →1.1 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)
X-Amz-Cf-Id →T_CC-vaqRAoxqnzFZdB9KMI9CAIPQvKAxCat2NPLyaJ5MPpdTVhF1g==
X-Cache →Miss from cloudfront
x-amz-apigw-id →JckIhHWmrPEFcqQ=
x-amzn-RequestId →675517fc-7eaa-11e8-8290-39c903c321e4
这是我一直在尝试调用 API 的代码,非常直截了当,我真的很困惑可能是什么错误:
$scope.fundAsset = function(assetID, userID){
$http({
method: 'POST',
url: 'https://xxxxxxxxxxx.execute-api.eu-west-2.amazonaws.com/dev/api/fund',
body: {
userID: userID,
assetID: assetID
}
}).then(function successCallback(response) {
console.log(response);
});
};