0

I am trying to use AngularJS to make a http get call:

<script>
function customersController($scope,$http) {
  $http.get('http://www.mee.wherego.ca/api/test/get')
  .success(function(response) {$scope.names = response;})
  .error(function(response) { 
      if (response) {$scope.result = response; } 
      else { $scope.result = 'null'; } });
}
</script>

From UI (Html code), the response is "null" which means response is undefined; however from firefox debugger, it returns a 200 status code but with the following error:

JSON SyntaxError

If I just type in the URL in Firefox's address bar, the JSON returned is:

   {"execution_result":"passed",
    "execution_timestamp":"2015-01-26 16:21:35.856",
    "exception":null,
    "message":null}

And JSONLint says this is a valid JSON.

In order to eliminate CORS possibility, I also tried to use Apache's CloseableHttpClient to make the get call, and that successfully returned the JSON. I think this tells me the CORS is not an issue.

I am not sure what is going on here; is it really something wrong with the JSON, or is it something wrong with http.get? I am really confused.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • The browser developer console should show you the entirety of the *actual* response you're getting. What does it look like? – Pointy Jan 26 '15 at 16:40
  • @Pointy that helps! It actually says CORS.. but I don't understand how come java http client can get the result while js http cannot... thanks though! – jamesdeath123 Jan 26 '15 at 16:52
  • Because CORS is purely a browser thing. The browser prevents JavaScript in a page from domain X from accessing domain Y if Y does not explicitly allow it. You can still type domain Y into the browser address bar, and you can access Y from any other HTTP client. – Pointy Jan 26 '15 at 17:07
  • @Pointy thank you very much for your answer! I think it would be great if you can post a simple CORS solution, and I will be happy to accept that as an answer! – jamesdeath123 Jan 26 '15 at 17:13
  • Well it depends on your web server as to you you configure it to deliver CORS headers; I'm not much of a server configuration person anyway. It's not something you can do from your client code (if there were, there wouldn't be much of a point to it :) – Pointy Jan 26 '15 at 17:15
  • If you want to bypass CORS you have a few options, but the easiest is to create a pass-through with a server that will feed your front-end data. Else, see this: http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy and https://www.npmjs.com/package/cors-anywhere and https://github.com/gr2m/CORS-Proxy – SoluableNonagon Jan 26 '15 at 19:12

0 Answers0