我有一个我正在开发的angular 6和aws-amplify / aws-amplify-angular应用程序,当前正在尝试执行HTTP GET。我收到的是一条通用的Error: Network Error
消息,从身份验证到与亚马逊的通讯问题,我的确可以是任何东西。
我的函数看起来像这样,我试图将我的lambda的响应简单地登录到控制台。
async getOwnerDecks() {
const authToken = await Auth.currentSession().then(data => {
return data.idToken.jwtToken;
});
const authHeader = {
Authorization: authToken
};
this.result = await this.amplifyService.api().get('decks', '', {headers: authHeader}).then( resp => {
return resp;
}).catch(err => {
console.log(err);
});
}
就是这样。我可以通过awsmobile awsmobile cloud-api invoke decks get /decks
的命令通过Postman调用并获得成功的响应,但是从上面的函数中我什么也没得到。我尝试使用API.get和angularService.api()。get都失败了。
Error
columnNumber: 15
config: Object { timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", … }
fileName: "http://localhost:8080/vendor.js"
lineNumber: 96651
message: "Network Error"
request: XMLHttpRequest { __zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://xxxxxx.execute-api.eu-west-1.amazonaws.com/MobileHub_Deployments/decks", __zone_symbol__ON_PROPERTYreadystatechange: handleLoad(), … }
response: undefined
stack: "createError@http://localhost:8080/vendor.js:96651:15\nhandleError@http://localhost:8080/vendor.js:96194:14\nwrapFn@http://localhost:8080/polyfills.js:3510:30\n./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:8080/polyfills.js:2743:17\nonInvokeTask@http://localhost:8080/vendor.js:40796:24\n./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:8080/polyfills.js:2742:17\n./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:8080/polyfills.js:2510:28\n./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:8080/polyfills.js:2818:24\ninvokeTask@http://localhost:8080/polyfills.js:3862:9\nglobalZoneAwareCallback@http://localhost:8080 /polyfills
这是我应该得到的答复
{
requestBody: null,
pathParams: '/decks',
queryStringParams: null,
headerParams:
{ Accept: 'application/json, text/plain, */*',
'CloudFront-Forwarded-Proto': 'https',
'CloudFront-Is-Desktop-Viewer': 'true',
'CloudFront-Is-Mobile-Viewer': 'false',
'CloudFront-Is-SmartTV-Viewer': 'false',
'CloudFront-Is-Tablet-Viewer': 'false',
'CloudFront-Viewer-Country': 'ES',
Host: 'xxxxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com',
'User-Agent': 'axios/0.17.1',
Via: '1.1 xxxxxxxxxxxxxxx.cloudfront.net (CloudFront)',
'X-Amz-Cf-Id': 'iNPucPQXZybc7Zg1AfA005rE-W30Qs-TG1V2pDK5fDHAPWSNSBg==',
'x-amz-date': '20180620T143059Z',
'X-Amzn-Trace-Id': 'Root=1-5b2a6523-3c88e3d2f77853e8cbf4351c',
'X-Forwarded-For': 'xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx',
'X-Forwarded-Port': '443',
'X-Forwarded-Proto': 'https' },
stage: 'Development',
stageVariables: { stage: 'Development' },
cognitoIdentityId: null,
httpMethod: 'GET',
sourceIp: 'xxx.xxx.xxx.xxx',
userAgent: 'axios/0.17.1',
requestId: '8cec8ff3-7496-11e8-a737-9d1996926e66',
resourcePath: '/decks'
}
我正在使用最新的6版本angular / cli,aws-amplify和aws-amplify-angular npm软件包。
我已验证它与cors不相关,因为以前我遇到过cors标头问题,现在,一旦我将所需的标头添加到awsmobile lambda中,该错误就消失了。
有什么明显的我想念吗?
答案 0 :(得分:0)
您具有以下代码:
this.result = await this.amplifyService.api().get('decks', '', {headers: authHeader}).then( resp => {
return resp;
}).catch(err => {
console.log(err);
});
似乎您在.get参数中传递了空路径。你应该有这样的东西吗...
this.result = await this.amplifyService.api().get('decks', '/decks', {headers: authHeader}).then( resp => {
return resp;
}).catch(err => {
console.log(err);
});
...正在传递/ decks路径而不是空字符串?