如何将多维数组返回到表中并在jquery中显示?
我通过ajax将多维数组从php脚本返回到html页面。现在数组中的值返回正确了。我在console.log上检查了它 但是如何在jquery中将每个数组更改为单个数组呢 这就是我传递表单php脚本的方式如何将多维数组返回到表中并在jquery中显示?,jquery,arrays,ajax,Jquery,Arrays,Ajax,我通过ajax将多维数组从php脚本返回到html页面。现在数组中的值返回正确了。我在console.log上检查了它 但是如何在jquery中将每个数组更改为单个数组呢 这就是我传递表单php脚本的方式 $json=array(); array_push($json,array("type"=>$carType,"maker"=>$carMaker,"rate"=>$selectRate)); echo json_encode($json); 这就是它在ajax中的处理方式
$json=array();
array_push($json,array("type"=>$carType,"maker"=>$carMaker,"rate"=>$selectRate));
echo json_encode($json);
这就是它在ajax中的处理方式
$("#submit").on("click",function()
{
$("#set_setting").submit(function(){
data = $(this).serialize()
$.ajax({
type: "POST",
dataType: "html",
url: "submit_setting.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//hide the form
$("#set_setting").slideUp("slow");
//show the result
console.log(data);
$(".the-return").html(data);//how to show in table here?
}
});
return false;
});
});
控制台。日志(数据)输出如下:
[
{
"type":["4 wheeler","flying ycar"],
"maker":["Honda","Audi"],
"rate":[["2","20"],["2","40"],["2","50"],["0","80"],["0","90"],["0","70"]
}
]
我想将类型、制造商和费率显示为单个表。
像
类型:四轮飞车[在选择框中]
制造商:本田、奥迪[选择框中]
比率:值2-20,40,50[表中]值0-80,90,70[表中]
我知道这有点复杂。至少if可以在不同的表中显示每个数组就足够了,但在Jquery中就足够了 编辑后
$("#submit").on("click",function()
{
$("#set_setting").submit(function(){
data = $(this).serialize()
$.ajax({
type: "POST",
dataType: "json",
url: "submit_setting.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//hide the form
$("#set_setting").slideUp("slow");
//show the result
var parse_JSON = function (data) {
try {
var obj = JSON && JSON.parse(data) || $.parseJSON(data);
return obj;
} catch (e) {
// not json
console.log("Can not parse");
return false;
}
};
$.each( obj.type, function( index, value ){
console.log(value);
});
//$(".the-return").html(me);
}
});
return false;
});
});
更新v2
在PHP脚本顶部添加以下标题:
header('Content-type: application/json');
更新:返回的JSON无效:
[
{
"type":["4 wheeler","flying ycar"],
"maker":["Honda","Audi"],
"rate":[["2","20"],["2","40"],["2","50"],["0","80"],["0","90"],["0","70"]
}
]
速率键缺少闭合方括号[
,它应该是:
[
{
"type":["4 wheeler","flying ycar"],
"maker":["Honda","Audi"],
"rate":[["2","20"],["2","40"],["2","50"],["0","80"],["0","90"],["0","70"]]
}
]
由于您希望得到JSON作为回报,因此,在AJAX请求中将数据类型更改为JSON:
$.ajax({
.....
dataType: "json",
.....
success: function(data) {
//hide the form
$("#set_setting").slideUp("slow");
$.each( data.type, function( index, value ){
console.log(value);
});
}
});
在AJAX回调中,可以这样循环遍历每个元素:
$.each( data.type, function( index, value ){
console.log(value);
});
注意
在这里键入
键和数据数组,将其更改为其他键以迭代其他数组元素参考以下链接
工作代码`
'var json_ec = $.parseJSON('{"type":["4 wheeler","flying ycar"],"maker":["Honda","Audi"],"rate":[["2","20"],["2","40"],["2","50"],["0","80"],["0","90"],["0","70"]]}');
var type = '';
var maker = '';
var rate = '';
$.each(json_ec,function(k,v)
{
if(k == 'type')
{
type = v;
}
else if(k == 'maker')
{
maker= v;
}
else if(k == 'rate')
{
rate = v;
}
});
console.log(type);
console.log(maker);
console.log(rate);'
`我试过了,结果显示Uncaught TypeError:无法读取未定义的属性'length'。不知何故无法访问类似data.type的任何内容。您使用的是哪个版本的jQuery?@Vani:您可以尝试从应答中添加的方法中筛选数据变量吗?在console.log()上看到了但没有看到任何内容您是否尝试循环此方法返回的数据?