gpt4 book ai didi

javascript - 如何在html中显示通过ajax传递的数组

转载 作者:行者123 更新时间:2023-12-02 15:58:52 25 4
gpt4 key购买 nike

这是我的ajax:

$("#submit").on("click",function()
{

$("#add_car_form").submit(function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "text",
url: "add_car_submit.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {

$(".the-return").html("<br />JSON: " + data );//this outputs

{"car_name":";ll","car_maker":"njk","car_type":"nkj","passanger":"1","rate":["89","67","45","34","23"],"action":"test"}

              }
});
document.getElementById("add_car_form").reset();
return false;
});

});

我只是从 php 脚本中回显,如下所示: echo $return["json"]; 它将输出如下:

{"car_name":";ll","car_maker":"njk","car_type":"nkj","passanger":"1","rate":["89","67","45","34","23"],"action":"test"}

如何以这样的 html 表格形式追加到 div 中?

Car Name: name
Car Maker: maker
......

最佳答案

在你的成功函数中尝试这个

data = jQuery.parseJSON( data );  //parse data if this is text other wise use data directly. hope this is a text param in your case.
var out = '<table>';
$.each(data, function( key, value ) {
out += '<tr><td>'+key+': '+ value +'</td></tr>';
});

out += '</table>';

//append out to your div
$(".the-return").html(out);

关于javascript - 如何在html中显示通过ajax传递的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com