Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AWS API网关JS SDK获取HTTP响应代码_Javascript_Amazon Web Services_Aws Api Gateway - Fatal编程技术网

Javascript AWS API网关JS SDK获取HTTP响应代码

Javascript AWS API网关JS SDK获取HTTP响应代码,javascript,amazon-web-services,aws-api-gateway,Javascript,Amazon Web Services,Aws Api Gateway,我正在使用API网关生成的JSSDK。问题是当出现错误时,我会收到CORS错误,并且我无法获取内部数据或状态代码: 请注意,尽管“网络”选项卡中有消息,但数据仍为“” 我的代码: AWS.config.region = "ap-northeast-2" const cognitoParams = { IdentityPoolId: "ap-northeast-2:...", Logins: { "accounts.google.com": googleUser.getAuth

我正在使用API网关生成的JSSDK。问题是当出现错误时,我会收到CORS错误,并且我无法获取内部数据或状态代码:

请注意,尽管“网络”选项卡中有消息,但数据仍为“”

我的代码:

AWS.config.region = "ap-northeast-2"
const cognitoParams = {
  IdentityPoolId: "ap-northeast-2:...",
  Logins: {
    "accounts.google.com": googleUser.getAuthResponse().id_token
  }
}

AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

AWS.config.credentials.get(function () {

  const api = apigClientFactory.newClient({
    accessKey: AWS.config.credentials.accessKeyId,
    secretKey: AWS.config.credentials.secretAccessKey,
    sessionToken: AWS.config.credentials.sessionToken
  })
  api.ec2Get()
    .then(function (data) {
      console.log('data', data);
    })
    .catch(function (err) {
      console.error('err', err)
    })

试着通过这个区域。。。因此,内部:

const api = apigClientFactory.newClient({
  accessKey: AWS.config.credentials.accessKeyId,
  secretKey: AWS.config.credentials.secretAccessKey,
  sessionToken: AWS.config.credentials.sessionToken,
  region: '...'
})

因为默认情况下,如果您不通过它,它将使用“us-east-1”

您确定AWS.CognitoIdentityCredentials确实在返回凭据吗?@UXDart是的,访问密钥和机密访问密钥中有数据。或者我还应该验证什么?你确定你需要API网关资源并将API部署到stage吗?嗨,这解决了安全令牌无效的问题,但我发现当令牌过期时,我仍然无法通过JS获取错误消息或状态,因为这是一个CORS错误。有什么想法吗?好的,但是CORS错误可能是因为确实存在CORS错误,或者是因为服务器返回的状态与200不同。。您在控制台中看到了什么?只有CORS错误?如果您收到状态403,那么您知道令牌已过期。如何从API网关生成的SDK错误中获取HTTP状态。从屏幕截图中可以看到,状态为0?
.catch(函数(err){if(err.status==403){}
您需要在出现错误的情况下进行检查……但问题是
err.status
在我的情况下是
0
,如屏幕截图所示