【问题标题】:How to show json data in codeigniter [duplicate]如何在codeigniter中显示json数据[重复]
【发布时间】:2015-11-04 17:47:56
【问题描述】:

我的来自 codeigniter json_encode 的 json 输出。这显示在控制台中,但我无法在 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');
            }
        
        });
    }

【问题讨论】:

  • WTH???一个关于迭代 JSON 数组的简单问题有这么多票……这应该有反对票

标签: javascript php jquery json codeigniter


【解决方案1】:
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');
        }

    });
}

【讨论】:

  • 我知道您已经评论了代码,但请不要发布仅代码的答案。
  • 谢谢@Oli Soproni B. ...你是救星:)
  • 这个问题已经在 SO 上得到回答。 OP 只是询问如何遍历简单的 JSON 数组,而您不应该回答这个问题。我们应该帮助人们,而不是为人们编写代码
【解决方案2】:

$("load_company_name") 不会返回 jQuery 对象。

选择器必须是类名、id 或其他一些有效的 CSS 选择器。 这将阻止您的 HTML 插入工作,因为它没有可插入的内容。

确保您想要的 jQuery 对象是有效的。喜欢,$('#load_company_name')


选择器排序后,您必须确保可以将 JSON 实际插入 HTML。使用JSON.parse()将其转换为JS对象,然后您可以遍历该对象并将其插入到HTML中。

类似:

var resultObj = JSON.parse(result);

for(item in resultObj) {
  if(resultObj.hasOwnProperty(item)) {
    $("#load_company_name").append('<span>' + item + '</span>');
  }
}

// Or use jQuery
var $resultObj = jQuery.parseJSON(result);

$resultObj.each(function() {
  $("#load_company_name").append('<span>' + this.company_name + '</span>');
});

【讨论】:

    【解决方案3】:

    在您的控制器中使用json_decode。并且不要忘记css和jquery的选择器。

    【讨论】:

    • 这应该是评论,而不是答案
    猜你喜欢
    • 2017-11-30
    • 2016-09-20
    • 1970-01-01
    • 2020-07-20
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多