2017-03-05 54 views
0

我有一个AWS API网关,它接受POST请求来触发Lambda函数并返回结果。Aurelia抓取AWS API网关提供不透明响应

我试图在API上启用CORS。

const url = 'XXX' 
@inject(HttpClient) 
export class App { 
    constructor(http) { 
    http.configure(config => 
     config 
     .useStandardConfiguration() 
     .withBaseUrl(url) 
     .withDefaults({ 
      // mode: 'no-cors', 
      headers: { 
      'Content-type' : 'application/json' 
      } 
     })); 

    this.http = http; 

    this.http.fetch('/', { method: 'post', body: {data: 'XXXX'} }) 
     .then(res => res.json()) 
     .then(console.log) 
     .catch(console.error) 
} 

结果是:

body:null 
bodyUsed:false 
headers:Headers 
ok:false 
status:0 
statusText:"" 
type:"opaque" 
url:"" 

这是我曾尝试:

  • 启用LAMBDA代理
  • 启用CORS
  • 使用选项端点预检响应。

我甚至试图改变拉姆达功能:

exports.handler = function(event, context, callback) { 
    callback(null, {"success": true}); 
} 

确保lambda函数“工程”(和它在可见的CloudWatch)

+0

据我了解Fetch规范中的要求,唯一可以用'type:“opaque”'结束的方法是如果[** response tainting **](https://fetch.spec。 whatwg.org/#concept-request-response-tainting)是不透明的。如果请求模式是“no-cors”,那么最终可能导致“不透明”响应的唯一方法。那么你的代码中实际上有'mode:'no-cors''注释掉了吗?如果不是,那么代码的其他部分似乎必须将请求模式设置为“no-cors”。否则,你不会得到'type:“opaque”'。 – sideshowbarker

+0

(你基本上不想故意发送'mode:'no-cors''在一个获取请求中(除非你正在做一些与Service Worker缓存有关的事情),例如,如果你这样做,你将无法设置'Content-type':'application/json'并让它工作,即使它在其他方面“起作用”,你也会得到一个回应,说你的JavaScript代码不能够做到除了缓存它之外的任何东西。) – sideshowbarker

+0

@sideshowbarker是的,我已经在我的代码中注释了'mode:'no-cors''。 – thisismytemp

回答

0

你不需要在Aurelia http中启用CORS,因为Aurelia Fetch客户端的默认值是CORS。我会说你的AWS API网关的问题是99%。你有没有试图从另一个应用程序获取你的端点? (例如使用Postman Chrome扩展)。它工作吗?