How to parse data from ajax to js?

advertisements

Basically I'm using Codeigniter to finish our company project, but the guy who was before me he code all dashboard on JS, I'm bad at JS and wondering how to parse data from Ajax inside the JS.

This is what my ajax looks like:

$.ajax({
    type: "POST",
    url: base_url + "user/get_clientid",
    dataType: "json",
    data: {
        user_submission_token: user_submission_token,
        first_name : fn
    },
    success: function(data) {
        dashboard.admin.client.cache.names(data.clients);
        dashboard.admin.client.table.names(data.clients);

        if($("#position").val() == 2)
            dashboard.admin.user.form.init();
        },
        error: function() { 

    }
})

From user/get_clientid url we are getting json like this:

{
    "first_name": [
        {
            "first_name": "Mahmood ",
            "client_id": "11"
        },
        {
            "first_name": "Mahmood ",
            "client_id": "14"
        },
        {
            "first_name": "Mahmood ",
            "client_id": "15"
        }
    ]
}

I have to parse first_name into these JS rows:

get_row: function(c,fn) {
            var row = "<div class=\"app-table-row col-lg-12\">"; // onClick=\"" + dashboard.admin.client.manage(c.id) + "\">";
            row += "<div class=\"app-table-col\" onClick=\"dashboard.admin.client.manage(" + c.id + ")\">" +' <img class="img-responsive" src= ' + c.photo_url + ' >' + "</div>";
            row += "<div class=\"app-table-col \" onClick=\"dashboard.admin.client.manage(" + c.id + ")\">" + HERE+ "</div>";
                            row += "<div class=\"app-table-col \"><a class=\"app-edit-icon\" title=\"Edit Client\" onClick=\"dashboard.admin.client.form.edit(" + c.id + ")\"></a>";
                            row += "<a class=\"app-trash-icon\" title=\"Delete Client\"  onClick=\"dashboard.admin.client.form.drop(" + c.id + ")\"></a></div>";

            row += "</div>";
            return row;
        }

first_name should be in second ROW where I put HERE.

Based on client.id but the who did this is already get client.id on c.id.

Hope this provided information will help you to understand my problem.


JSON.parse will convert your json into a JS object.