Freddy
Freddy

Reputation: 970

Ajax doesn't return JSON but an object

I want to load data from WCF to a grid (SlickGrid). This code works if I manually make a Json and load it in the grid. But if I use the variable I get from Jquery - ajax I get an object insted of an array.

If I run the following code.

$(function () {
    $.ajax({
        url: "DataService.svc/DoWork",
        type: "POST",
        contentType: "application/json",
        dataType: 'JSON',
        success: loadData,
        error: showError
    });
});

function loadData(result) {
    var data = [];
        data[0] = {
            BO: "Task ",
            Agreement: "5 days"
        };
    console.log(data);
    console.log(result);
    grid = new Slick.Grid("#myGrid", data, columns, options);
}

I have 2 console logs. The first log is the Json I made. The second is the Json Ajax returns. Chrome tells me this is an object instead of an array(Json).

How can I convert the object (result) to a array structure like data? enter image description here

Upvotes: 1

Views: 378

Answers (3)

Freddy
Freddy

Reputation: 970

I solved it by adding a .d after result. This returns the right object for the SlickGrid.

function loadData(result) {
    grid = new Slick.Grid("#myGrid", result.d, columns, options); 
}

Upvotes: 0

Cracker0dks
Cracker0dks

Reputation: 2490

var data = [];
    for(var i in result) {
        data.push(result[i]);
    }

(if D is not enough)

Upvotes: 1

Lucky Lefty
Lucky Lefty

Reputation: 347

try changing line:

var data = [];

to:

 var data = new Array();

inside function loadData

Upvotes: 1

Related Questions