0

I'm new in AngularJS, and i'm trying to paginate an json result, but i have an error. The JSON service returns something like the that:

products.json:

 X({
          "data":{
             "reference":{
                "timestamp":"24/02/2014",
                "item":{
                   "id":"123456",
                   "name":"Reference Product",
                   "price":"R$ 39,90",
                }
             },
             "arrayData":[
                {
                   "businessId":"123",
                   "name":"Recommended Product",
                   "price":"R$ 29,00",
                },
                {
                   "businessId":"1234",
                   "name":"Another Recommended Product",
                   "price":"R$ 39,00",
                },
            ]
        }
    });

And my script that process the json is the following:

var todos = angular.module('results', ['ui.bootstrap']);

results.controller('ResultsController', function($scope, $http) {
    $scope.makeResults = function() {
        $http.get('products.json').success(function(resp){
            console.log(resp);
        });
    };

    $scope.makeResults();
});

But when executes the code is printed in the console a plain text, not an object. If I use $http.jsonp instead $http.get, a ReferenceError: X is not defined is apresented. For test only, in jQuery ajax I've tryed the same with the dataType: 'jsonp' and jsonpCallback: "X" options and got resolved the problem.

Can anyone help me?

Thks

  • how about your `products.json` look likes ? – underscore Feb 24 '15 at 16:14
  • 5
    The content of `products.json` is definitely not valid JSON, it's JavaScript. It follows the conventions of JSONP. If you want to use JSONP, then you also have to define the function on the client side (`X`). If there is not reason to use JSONP, then just put valid JSON into your file and parse it with `JSON.parse`. You should probably read [What is JSONP all about?](http://stackoverflow.com/q/2067472/218196) – Felix Kling Feb 24 '15 at 16:14
  • @FelixKling ...The JSON file is a service and i cant access the generator of this JSON result. How can I define the X function? – Thevans Vilella Feb 24 '15 at 17:59
  • @underscore the content of products.json is in the topic... the first code block – Thevans Vilella Feb 24 '15 at 18:00
  • *"How can I define the X function?"* Like you define any other function: `function X(data) { /* do stuff with data */ }`. It must be global. However, I'd assume that Angular has a way to work with JSONP without you having to define the function manually. https://docs.angularjs.org/api/ng/service/$http#jsonp – Felix Kling Feb 24 '15 at 18:05

0 Answers0