将php数组发送到jquery ajax并从该数组中创建每个循环

时间:2021-06-27 13:39:46

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

嗨实际上我读了很多关于数组和jquery的话题,他们都在hmtl脚本标签里面显示了使用jquery的例子,但我试着学习的是如何读取php发送的数据通过jj文件中的ajax

here is my php example

这是我的php示例

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

then here is my ajax

那么这是我的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

谢谢,如果你能帮助我

7 个解决方案

#1


4  

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]);
             }
        }
    });
 }

#2


2  

Try a for loop to loop the records

尝试使用for循环来循环记录

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

        });
    },
});

#3


2  

Please use below code :

请使用以下代码:

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

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

这里每个循环的响应数组和值get('jazz',rock,...等)。

#4


2  

try see this, clear solution for your question

试试这个,明确解决你的问题

https://*.com/questions/20772417/how-to-loop-through-json-array-in-jquery

https://*.com/questions/20772417/how-to-loop-through-json-array-in-jquery

#5


2  

$.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

$ .each:通用迭代器函数,可用于无缝迭代对象和数组。具有length属性的数组和类数组对象(例如函数的参数对象)由数字索引迭代,从0到length-1。其他对象通过其命名属性进行迭代。参考文档

$.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;
}

#6


1  

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

您可以通过在jQuery中使用.each来获取数组索引和值:

$.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
        });
    },
});

#7


1  

<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>

#1


4  

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]);
             }
        }
    });
 }

#2


2  

Try a for loop to loop the records

尝试使用for循环来循环记录

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

        });
    },
});

#3


2  

Please use below code :

请使用以下代码:

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

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

这里每个循环的响应数组和值get('jazz',rock,...等)。

#4


2  

try see this, clear solution for your question

试试这个,明确解决你的问题

https://*.com/questions/20772417/how-to-loop-through-json-array-in-jquery

https://*.com/questions/20772417/how-to-loop-through-json-array-in-jquery

#5


2  

$.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

$ .each:通用迭代器函数,可用于无缝迭代对象和数组。具有length属性的数组和类数组对象(例如函数的参数对象)由数字索引迭代,从0到length-1。其他对象通过其命名属性进行迭代。参考文档

$.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;
}

#6


1  

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

您可以通过在jQuery中使用.each来获取数组索引和值:

$.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
        });
    },
});

#7


1  

<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>