在ajax请求中加载视图并使json数据可用(Codeigniter)

时间:2015-05-04 03:20:28

标签: php jquery ajax json codeigniter

我希望ajax请求加载视图以及使json数据可用于相同的加载视图。在下面的ajax请求中,我正在请求加载视图的letUsKnow方法,并且我希望json数据在相同的加载视图中可用。我尝试了下面的代码,但它不起作用。

$(document).on("click", "#letKnow", function(){
        $.ajax({
        'type': "POST",
        'url': base_url + "init/letUsKnow",    //calling method in controller
        'data': {},
        success: function (response)
        {
            $("#ads_container").html(response.html);
            alert(response.data[0].model_name);     //undefined
        },
        complete: function()
        {

        }
    });
});

init(控制器)

 public function letUsKnow() {
    $models = $this->dbmodel->get_models();
    $data = json_encode($models);
    $this->load->view("letUsKnow",$data);

}

可以从单个ajax请求获取此功能。我可以使用两个ajax请求,在从一个ajax请求加载视图后再使用另一个ajax来请求json数据。但是,如何通过单个ajax请求实现此目的。

修改

您可以将整个回复转换为json。

$this->load->view()有第三个参数,允许您将视图作为字符串返回给变量。

我不明白为什么你是json编码$models传入视图所以我假设你想要它作为主响应的一部分

PHP

 public function letUsKnow() {
    $models = $this->dbmodel->get_models();

    $output = array(
        'html'=>$this->load->view("letUsKnow",$models, true),
        'data' = >$models
    );
    // should set Content Type header for json here - see output docs
    echo json_encode($output);
}

数组($ models)

 // $models after json encode in ajax response
     {"abc":[{"id":"76","brand_id":"23","model_name":"iphone 4"},{"id":"77","brand_id":"23","model_name":"iphone 4s"}]}

 // after making array of views and data and after json encode in ajax response I get:
      {"html":"this is html views <\/div>\r\n<\/div>\r\n\r\n\r\n\r\n",
       "data":{"abc":[{"id":"76","brand_id":"23","model_name":"iphone 4"},{"id":"77","brand_id":"23","model_name":"iphone 4s"}]}} 

 // but in real my view's html is:
      <div>this is html views</div><div id="iop"></div>    //html in controller

 // also when I tried to access json object like:
      response.hm   // I get undefined
      response.data.model_name    //I get undefined.

AJAX

$.ajax({
    type: "POST",
    url: base_url + "init/letUsKnow", //calling method in controller
    data: {},
    dataType:'json',
    success: function (response) {
        var modelsData = response.data;             //undefined
        $("#ads_container").html(response.html);    //undefined
    },
    error: function(){ alert('Ooops ... server problem'); }
});  

2 个答案:

答案 0 :(得分:2)

您可以将整个回复转换为json。

$models有第三个参数,允许您将视图作为字符串返回给变量。

我不明白为什么你是json编码 public function letUsKnow() { $models = $this->dbmodel->get_models(); $data = ?? ; // why is it json_encoded? $output = array( 'html'=>$this->load->view("letUsKnow",$data, true), 'data' = >$models ); // should set Content Type header for json here - see output docs echo json_encode($output); } 传入视图所以我假设你想要它作为主要响应的一部分

PHP

$.ajax({
    type: "POST",
    url: base_url + "init/letUsKnow", //calling method in controller
    data: {},
    dataType:'json',
    success: function (response) {
        var modelsData = response.data;
        $("#ads_container").html(response.html);
    },
    error: function(){ alert('Ooops ... server problem'); }
});  

AJAX

FashionFrenzy

答案 1 :(得分:1)

    //PHP
    public function letUsKnow() {
        $models = $this->dbmodel->get_models();
    $data =[];// why is it json_encoded?
            header( "Content-Type: application/json" );
        $output = array(
            'html'=>$this->load->view("letUsKnow",$data, true),
            'data' = >$models
        );
        // should set Content Type header for json here - see output docs
        echo json_encode($output);
return;
    }


    //AJAX
    $.ajax({
        type: "POST",
        url: base_url + "init/letUsKnow", //calling method in controller
        data: {},
        dataType:'json',
        success: function (response) {
            var modelsData = response.data;
            $("#ads_container").html(response.html);
        },
        error: function(){ alert('Ooops ... server problem'); }
    });