简体   繁体   中英

Can't retrieve data in table from json

I am trying to load data into table using JQuery and AJAX but when I click on the button data is not retrieved. I have done coding as shown below:

var globalgrid;

Here I am going to call json url and try to display it in the table.

function loadgrid() {
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: 'http://instatalk.in/sip/GetApprovedUsersList?page=1&limit=10',
        success: function(griddata) {
            globalgrid = griddata.lines;

            // remove all data - but the headers!
            $("#gridtable").find("tr:gt(0)").remove();

            if (globalgrid.length === 0) {
                $('#errormsg').html('Sorry, <strong>no</strong> rows returned!');
                return;
            }

            for (var i = 0; i < globalgrid.length; i++) {
                var line = globalgrid[i];
                // insert after last row!
                $('#gridtable > tbody:last').append('<tr><td>' + line.Id + '</td><td>' + line.AccountId + '</td><td>' + line.Name + '</td><td>' + line.IsFranchiseUser + '</td></tr>');
            }
        },
        error: function(data, errorText) {
            $("#errormsg").html(errorText).show();
        }
    });
}

I am getting the table heading only. When I click on the button I want data to be retrieved from the json data. I don't know where I am going wrong. Please do help.

This is my json file:

{"results":[{"Id":17,"AccountId":"5737329468","Name":"Martin (Nigeria)","IsFranchiseUser":false},{"Id"
:16,"AccountId":"3644824444","Name":"Deep Patel","IsFranchiseUser":false},{"Id":15,"AccountId":"4692068407"
,"Name":"Jacob (kiribati)","IsFranchiseUser":false},{"Id":14,"AccountId":"4650982975","Name":"sebin John
 (spain)","IsFranchiseUser":false},{"Id":13,"AccountId":"2855375107","Name":"Jassi want(new jersey)"
,"IsFranchiseUser":false},{"Id":12,"AccountId":"6242007588","Name":"Moussa","IsFranchiseUser":false}
,{"Id":11,"AccountId":"3075258818","Name":"srkrbm (saudi arab)","IsFranchiseUser":true},{"Id":10,"AccountId"
:"3615509810","Name":"Om Saini","IsFranchiseUser":false},{"Id":9,"AccountId":"9251133143","Name":"swati
 mohandas","IsFranchiseUser":false},{"Id":8,"AccountId":"8143395019","Name":"babu Kuppu","IsFranchiseUser"
:false}],"totalAccounts":16}

This line: globalgrid = griddata.lines;

According to your Json data format, you should use griddata.results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM