简体   繁体   中英

Send php array to jquery ajax and make a each loop from that array

hi actually i read many topic about array and jquery and they all show example using jquery inside the hmtl script tag but what i try to learn is how to read an array sent by php throught ajax inside a js file

here is my php example

$record = array('jazz','rock', 'punk', 'soft', 'metal');
echo json_encode($record);

then here is my ajax

$.ajax({
    url: "system/music_list.php",
    dataType: 'json',
    cache: false,
    success: function(response){
        // here i want to read the array and make a loop for each element 
    },
});

thanks if you can help me

try basic for loop with count using length This .. this should help surely.

 function ajax_test()
 {
    $.ajax({
        url: "system/music_list.php",
        dataType: 'json',
        cache: false,
        success: function(response)
        {
             for (var i = 0; i < response.length; i++)
             {
                 alert(response[i]);
             }
        }
    });
 }

Please use below code :

$(response).each(function(key,value){
    console.log(value);
});

Here each loop of response array and value get ('jazz',rock,...etc).

Try a for loop to loop the records

$.ajax({
    url: "system/music_list.php",
    dataType: 'json',
    cache: false,
    success: function(response){
        var record;
        for(record in response)
        {
            console.log(response[record]); 

        });
    },
});

$.each : A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties. Reference documentation

$.ajax({
url: "system/music_list.php",
dataType: 'json',
cache: false,
success: function(response){
 //Check if the response is in expected JSON format.
  var flag = isJSON(response);
    if(flag === true)   
    { response = JSON.parse(response);  }              
    //Iterate the Array using for each loop of jquery
    $.each(response, function( index, value ) {
      console.log( "Index : " + index + "Value : " + value );
    });
  } // End of success function
}); //End of Ajax

//JSON format check
function isJSON(data) {
   var ret = true;
   try {
      JSON.parse(data);
   }catch(e) {
      ret = false;
   }
   return ret;
}

You can get array indexes and values by using .each in jQuery as:

$.ajax({
    url: "system/music_list.php",
    dataType: 'json',
    cache: false,
    success: function(response){
        $.each(response, function(index,value)
        {
            console.log(index); // print all indexes
            console.log(value); // print all values
        });
    },
});
<div id="dat" name="dat"></div>
<script type="text/javascript">
    $.ajax({ url: "music_list.php",
             dataType: 'json',
             cache: false,
             success:function(response) { 
                 for( res in response) {
                     document.getElementById('dat').innerHTML+=response[res]+"<br/>"; 
                 }
             }
         });
</script>

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