Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want ajax request to load view as well as make json data available for same loaded view. In bellow ajax request, I am making request to letUsKnow method which loads views and also I want json data to be available in same loaded views. I tried following code but its not working.

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

}

Can this functionality be obtained from single ajax request. I can do with two ajax request that after loading view from one ajax request and again another ajax to request json data. But, How to fulfill this with single ajax request.

Edit

You can turn the whole response into json.

$this->load->view() has a third argument that lets you return the view as a string to a variable.

I don't understand why you are json encoding $models to pass into the view so I will assume you want that as part of the main response

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'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'); }
});  
like image 301
user254153 Avatar asked Nov 27 '25 23:11

user254153


2 Answers

You can turn the whole response into json.

$this->load->view() has a third argument that lets you return the view as a string to a variable.

I don't understand why you are json encoding $models to pass into the view so I will assume you want that as part of the main response

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'); }
});  
like image 58
charlietfl Avatar answered Nov 30 '25 11:11

charlietfl


    //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'); }
    });
like image 21
usman Avatar answered Nov 30 '25 13:11

usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!