Error importing Dynamic Json via Angularjs

Asked

Viewed 388 times

4

Hello, I’m having trouble importing this Json and others, I don’t know what I might be doing wrong in that...

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  
  <script type="text/javascript">
    var app = angular.module('myApp', []);
    app.controller('heroisCtrl', function($scope, $http) {
       $http.get("https://angularjs.org/greet.php?callback=herois&name=Super%20Hero")
       .success(function(data) { $scope.names = data;});
    });
    </script>
</head>
<body>

<div ng-app="myApp" ng-controller="heroisCtrl">
<table>
  <tr ng-repeat="x in names">
    <td>{{ x.name }}</td>
    <td>{{ x.greeting }}</td>
  </tr>
</table>
</div>

</body>
</html>

  • This error appears on the console: Xmlhttprequest cannot load https://angularjs.org/greet.php?callback=herois&name=Super%20Hero. No 'Access-Control-Allow-Origin' header is present on the requested Resource.

1 answer

4


Looks like you’re mixing up some stuff from the Angular documentation, I’ll try to explain the problems:

  1. This url you are using returns JSONP to bypass the access restriction policy for another domain. To receive data in this format, you need to use the method $http.jsonp instead of $http.get.

  2. According to the documentation of that method, the parameter callback url must have value JSON_CALLBACK, and not herois as you are using. Otherwise Angular will not know how to treat the result (if you want to keep it your way, you would need to have a function herois to deal with the outcome).

  3. The data returned by this URL come like this:

    {"name":"Super Hero","salutation":"Dia Duit","greeting":"Dia Duit Super Hero!"}
    

    That is, it is an object, not an array. But your view code is treating it like an array of objects, which would create a row in the table for each object with the ng-repeat. This will not work with this data, you would need to eliminate the repeat and take the properties directly from the object.

Fixing these issues, your code would look like this:

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  
  <script type="text/javascript">
    var app = angular.module('myApp', []);
    app.controller('heroisCtrl', function($scope, $http) {
       $http.jsonp("https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero")
       .success(function(data) { $scope.dados = data;});
    });
    </script>
</head>
<body>

<div ng-app="myApp" ng-controller="heroisCtrl">
<table>
  <tr>
    <td>{{ dados.name }}</td>
    <td>{{ dados.greeting }}</td>
  </tr>
</table>
</div>

</body>
</html>

  • Perfect! Following this logic then and understanding the corrections according to the Angular documentation for this case, why can I not do the same of the above example with another api? http://www.folhacar.com.br/frontendnovo.php/api/listMarcas?callback=JSON_CALLBACK for example. I tried to print as {{ data.name_tag }} but do not print anything.

  • Do you mean fetching data from another API? It may not be returning JSONP. And it may not allow access with $http.get (if this is the case, an error appears in the browser console, check this).

  • If I use it. get returns 'Access-Control-Allow-Origin' really, but if I use JPONP as you guided me it does not present any error, but it does not print anything, either on the page or on the console. Anyway... my reasoning about searching in another api and printing as {{data.field}} is right then? If so I will need to contact the developers of the company backend and say about these issues.

  • If the API does not provide JSONP output, there is no point in trying to use JSONP. One option is to create a proxy using some server language (for example, with PHP and Curl), and access by ajax your own script, which in turn accesses the API.

  • Great! Thank you very much @bfavaretto ! It was very helpful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.