简体   繁体   中英

dealing with ajax response from php in jquery

I am trying to get the data from the array in foreach . I am sending a ajax get request to php file and get response with json & array. But when I try to print what inside the array its broke..

My JS code:

        var url = $("#url").val();
        var type = 'F';
        var data_url = url + "manage/sources/ajax/ajax.php?type=GetBarber&gender=" + type ;
        $.ajax({
            type: "GET",
            url: data_url,
            dataType: "json",
            success: function (response) {
             // response returning - > {"status":"success","data":["name541","name214"]}
            var json_obj = $.parseJSON(response);
             for (i=0; i < json_obj.data.length; i++)
                  {
                    var payment = json_obj.data[i];
                    console.log(payment); // Just for debugging
                  }


                }
                }); 

I am trying to print what inside the data (names..) Thank you for help

You have the dataType set to json therefore response is already parsed to an object. So do not call $.parseJSON

for (i=0; i < response.data.length; i++)
{
    var payment = response.data[i];
    console.log(payment); // Just for debugging
}

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