gpt4 book ai didi

javascript - 如何在codeigniter中显示json数据

转载 作者:可可西里 更新时间:2023-11-01 12:54:06 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I iterate over a JSON structure? [duplicate]

(13 个回答)


6年前关闭。




我的 json 输出来自 codeigniter json_encode。这显示在控制台中,但我无法在 html 格式的页面上显示它

[
{"company_name":"Jalalabad Ragib-Rabeya Medical College"},
{"company_name":"Jalalabad Ragib-Rabeya Medical College"}
]

这是javascript。我怎样才能读取那些 json 数据并显示一个列表
function getDocCat(catId){     
var currentValue = catId.value;

$.ajax({
type: "POST",
url: "<?php echo site_url('home/get_com_by_cat') ?>",
data: { data: currentValue },
dataType:'json',
success: function(result){
$("load_company_name").html(result);
},
error: function() {
alert('Not OKay');
}

});
}

最佳答案

function getDocCat(catId){ 

var currentValue = catId.value;

$.ajax({
type: "POST",
url: "<?php echo site_url('home/get_com_by_cat') ?>",
data: { data: currentValue },
dataType:'json',
success: function(result){

// since the reponse of the sever is like this
// [
// {"company_name":"Jalalabad Ragib-Rabeya Medical College"},
// {"company_name":"Jalalabad Ragib-Rabeya Medical College"}
// ]
// then you can iterate on the array

// make sure you have a html element that
// has an id=load_company_name
var company = $("#load_company_name");


// here is a simpe way
$.each(result, function (i, me) {
// create a p tag
// insert it to the
// html element with an id of load_company_name
var p = $('<p/>');
// you can access the current iterate element
// of the array
// me = current iterated object
// you can access its property using
// me.company_name or me['company_name']
p.html(me.company_name);
company.append(p);
});
},
error: function() {
alert('Not OKay');
}

});
}

关于javascript - 如何在codeigniter中显示json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31959778/

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