0

I am using https://github.com/ManifestWebDesign/angular-gridster. Here is Code for default chart sizes :-

$scope.dashboards = {
  '1': {
    widgets: [{
      row: 0,
      col: 0,
      sizeX: 2,
      sizeY: 1,
      name: "Chart1",
      canvas: "canvas1"
    }, {
      row: 0,
      col: 2,
      sizeX: 2,
      sizeY: 1,
      name: "Chart2",
      canvas: "canvas2"
    }]
  }
};

I want to store this default sizes in a database & call it on load & apply it to $scope.dashboards.

So i stored the data in database column like this :-

[{row: 0,col: 0,sizeX: 2,sizeY: 1,name: "Chart1",canvas: "canvas1"}, {row: 0,col: 2,sizeX: 2,sizeY: 1,name: "Chart2",canvas: "canvas2"}]

And after ajax call :-

jq.ajax({
    type: "GET",
    url: "/Method/",
    success: function(data) {

        var arr = JSON.parse(data);

        $scope.dashboards = {
            '1': {
                widgets: arr
            }
        };
    }
});

Gives me error :-

SyntaxError: Unexpected token r
    at Object.parse (native)

2 Answers 2

1

JSON.parse() is very strict in grammer,as the pair of menber of an object,it should be

string:value , so the "first" and "second" should be string as json. change your json to following code and it should be right

'[{"row": "0","col": "0","sizeX": "2","sizeY": "1","name": "Chart1","canvas": "canvas1"},
  {"row": 0,"col": 2,"sizeX": 2,"sizeY": 1,"name": "Chart2","canvas": "canvas2"}]

it will work this question is answered before

or add

dataType:"json" to your function in case you will need not to parse this object

jq.ajax({
    type: "GET",
    url: "/Method/",
    dataType:"json",
    success: function(data) {

        var arr = data;

        $scope.dashboards = {
            '1': {
                widgets: arr
            }
        };
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

It seems like ajax intelligent guess already treats data as a json and transforms it into an object, so you don't have to parse it once again. This statement: var arr = JSON.parse(data); is redundant. Try:

jq.ajax({
    type: "GET",
    url: "/Method/",
    success: function (data) {        
        $scope.dashboards = {
            '1': {
                widgets: data
             }
        };
    }
});

Comments

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.