0

So I've created a super basic NoSQL DB in DynamoDB and used API Gateway to access the data. I created a pretty simple Get request that retrieves documents based on it's ID. Now I'm fairly new to Angular and AWS so I'm kind of in over my head right now. I've played around with different code and found a pretty basic angular example that runs a $http.get and pretty much just displays the data in a window. My problem is that when I run this angular code with any other API endpoint it works great. However, when I run this code against my API Gateway endpoint nothing pulls through. I've tried adding a content-type header but that still doesn't get me anything. Pretty stuck and any help would be appreciated. I'm sure it's something obvious I missed on the API Gateway/my rest call but anything help would be great! Thanks in advance!

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl"> 


<h1>{{myWelcome}}</h1>

</div>


<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("https://xyz.execute-api.us-west-2.amazonaws.com/dev/idex/0001")
 .then(function(response) {
  $scope.myWelcome = response.data;
  });
});
</script>

</body>
</html>

Obviously the json should show up through the H1 tags but nothing is coming through. Also, I've changed my endpoint url for security.

6
  • Do you have any error on the browser console? Commented Jun 28, 2016 at 18:59
  • Two errors. First: 15:02:43.958 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [ API ENDPOINT URL ]. (Reason: CORS header 'Access-Control-Allow-Origin' missing).1 <unknown>. Second: GET XHR [ API Endpoint ] [HTTP/1.1 407 Proxy Authentication Required 17ms] Commented Jun 28, 2016 at 19:03
  • CORS error, means that you should be using the same URL to access your resource: en.wikipedia.org/wiki/Cross-origin_resource_sharing Commented Jun 28, 2016 at 19:13
  • I enabled CORS on the GET method in API Gateway but do I have to include the headers in the GET request? Commented Jun 28, 2016 at 19:27
  • You just enabled it or you did it before and it didn't worked? Commented Jun 28, 2016 at 19:34

1 Answer 1

3

First, I highly recommend that you test your API with curl first to make sure it's working as expected in the simple case before trying to get it to work when called from Angular via a browser.

As already mentioned in some of the comments on your question, you will need to enable CORS for this method. Be sure to re-deploy your API after enabling CORS. Detailed instructions below.

In the API Gateway console, select your GET method. From the "Actions" dropdown button, select "Enable CORS". In the bottom right, click on the button labeled "Enable CORS and replace existing CORS headers". After this finishes, select "Deploy API" from the "Actions" drop down button to re-deploy the API. (You have to deploy again after enabling CORS.)

Sign up to request clarification or add additional context in comments.

1 Comment

Yep that did the trick! Didn't think to re-deploy it but makes perfect sense. Thanks mikeD at AWS and @Rida BENHAMMANE for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.