Php 如何在jquery中从json数组读取数据?
我必须从MySQL中检索许多行,并使用ajax编码发送,我做到了这一点。但问题是我无法在ajax中处理输出数组数据。有人能帮我吗 1> one.phpPhp 如何在jquery中从json数组读取数据?,php,jquery,mysql,ajax,json,Php,Jquery,Mysql,Ajax,Json,我必须从MySQL中检索许多行,并使用ajax编码发送,我做到了这一点。但问题是我无法在ajax中处理输出数组数据。有人能帮我吗 1> one.php <?php $host = "localhost"; $user = "root"; $pass = ""; $databaseName = "elearning"; $tableName = "users"; $con = mysql_connect($host,$user,$pass);
<?php
$host = "localhost";
$user = "root";
$pass = "";
$databaseName = "elearning";
$tableName = "users";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
if(isset($_POST)){
$exam_id=$_POST['exam_id'];
$sql="select * from exam_to_question where exam_id=$exam_id";
$result = mysql_query($sql);
$dataArray = array();
while($array = mysql_fetch_assoc($result)){
$dataArray[] = $array;
}
echo json_encode($dataArray);
}
?>
如果这是一个数组,则必须使用jQuery的
.each()
方法:
$.each(data, function(i, resp){
console.log(resp);
});
您将获得带有ajax响应的jquery对象。因此,您可以使用以下任何功能处理它:
如果您使用了dataType:json,那么您可以直接使用
//if it is not a multidimensional array then you can dirctly
data.keyName
//if it is multidimensional array
$(data).each(function(index,element){
console.log(element);
})
你能发布你的JSON数据吗?你可以调查一下。还具有良好的读取警报(数据);给我输出[对象对象],[对象对象],[对象对象对象],[对象对象对象],[对象对象对象]。学习使用
console.log
read。当我发出警报“警报(resp);”时,它给我[对象对象对象]@代码崩溃尝试从ROW警告resp.exam\u id或其他字段谢谢先生。我尝试警告(resp['question\u id');完成了!:)再次感谢!
//if it is not a multidimensional array then you can dirctly
data.keyName
//if it is multidimensional array
$(data).each(function(index,element){
console.log(element);
})