2016-08-02 91 views
0

我是AWS新手。试图在AWS中使用API​​。这是一个AJAX请求,响应类似于{“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“error”}AWS - API调用 - jquery ajax错误

我试图从我的本地主机使用API​​。很少有帖子称AWS不允许跨域请求。我如何在本地测试它?

 var postURL= "http://example.com/user/" + txtMemberId; 

     $.ajax({ 
      url: postURL, 
      crossDomain: true, 
      datatype: "text", 
      success: function (data, textStatus, xhr) { 
       console.log(data); 
      }, 
      error: function (xhr, textStatus, errorThrown) { 
       console.log(xhr); 
      } 
     }); 

状态是(OK = 200),这是一个有效的响应,如果我直接在https://console.aws.amazon.com/apigateway到API网关控制台给人网址在浏览器

+0

AWS确实允许跨域请求。这个API正在运行什么服务? AWS API网关? EC2?如果它只是一个在EC2实例上运行的应用程序,那么这将成为Apache或Nginx问题,而不是AWS问题。您需要提供更多详细信息,以便我们为您提供启用CORS的说明。 –

+0

@MarkB将应用程序部署到Elastic Bean Stalk – gunner

+0

然后,您需要在运行Elastic Beanstalk上的API的Web服务器上启用CORS。 –

回答

1

1点登录击中API。

2 - 在API网关控制台中,在API下选择一个API。

3-在资源下选择一个资源。这将启用资源上所有方法的CORS。

4或者,您可以在资源下选择一种方法来为此方法启用CORS。

5 - 从Actions下拉菜单中选择Enable CORS。

了解更多信息:

http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html

enter image description here

+0

他没有使用API​​网关。 –