2017-05-27 80 views
0

我正在使用由API網關生成的JS SDK。問題是,即使世界上的錯誤,我得到一個CORS錯誤,我無法得到裏面的數據也不是狀態代碼:AWS API網關JS SDK獲取HTTP響應代碼

enter image description here

公告信息是「」儘管有是在網絡選項卡中的消息

enter image description here

我的代碼:

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) 
    }) 
+0

你確信AWS.CognitoIdentityCredentials真的返回憑據? – UXDart

+0

@UXDart是存在訪問密鑰和祕密訪問密鑰中的數據。或者我還需要驗證什麼? –

+0

您確定您的API網關資源已啓用[CORS](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html#how-to-cors-console),以及部署你的API到舞臺? –

回答

1

嘗試通過區域太...所以裏面:

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

因爲默認情況下它使用「美東-1」,如果你不把它傳遞

+0

嗨,這解決了有一個無效的安全令牌的問題,但我發現,當令牌到期時,我仍然無法通過JS獲取錯誤消息或狀態,因爲它的CORS錯誤。有任何想法嗎? –

+0

好的,但是CORS錯誤可能是因爲確實存在CORS錯誤,或者是因爲服務器返回的狀態不是200 ..您在控制檯中看到了什麼?只有CORS錯誤?如果您收到狀態403,那麼您知道該令牌已過期 – UXDart

+0

如何從API網關生成的SDK錯誤中獲取HTTP狀態。從屏幕截圖中可以看到,狀態爲0? –