Load view and make json data available in ajax request (Codeigniter)

I want the ajax request to load the view and also make the json data available for one loaded view. In the next ajax request I am accessing the method letUsKnow

that loads the views and also I want the json data to be available in the same loaded views. I tried the following code but didn't work.

$(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 (controller)

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

}

      

Is it possible to get this functionality from a single ajax request. I can do with two ajax requests, that after loading the view from one ajax request and another ajax again to request the json data. But, how to accomplish this with a single ajax request.

Edit

You can include the entire response in json.

$this->load->view()

has a third argument that allows you to return a string representation of a variable.

I don't understand why you are json encoding $models

going to the view, so I assume you want this to be part of the main answer

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);
}

      

array ($ 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 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'); }
});  

      

+3


source to share


2 answers


You can include the entire response in json.

$this->load->view()

has a third argument that allows you to return a string representation of a variable.

I don't understand why you are json encoding $models

going to the view, so I assume you want this to be part of the main answer

PHP



 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);
}

      

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'); }
});  

      

+2


source


    //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'); }
    });

      



+1


source







All Articles