Loading view after ajax call, CodeIgniter

I have an ajax call from javascript for a method in the controller, the method in the controller should load the view (for example, show a new page on the screen). Although everything looks fine, the new view does not seem to be loading onto the screen. I cannot use the title or windows.location because I am passing an array variable containing the data to be used in the view.

The page is displayed on the Network tab (Preview tab when ajax is selected) of the Chrome debugging console. But the main screen remains as it is.

If someone has encountered a similar problem or has a solution, please help me.

Thank!!

+5
source share
3

, .

ajax, .

$this- > load- > view ('pagename', $datapassed); .

,

$data=$this->load->view('pagename',$datapassed, TRUE);

, $data ,

$this->set_output($data); 

ajax div.

,

$(body).html(result);

, html, .

+10

function get_view_ajax()
{
   $data['username] = $_POST['username];
   $response = $this->load->view('radius/radius_corporate_graph',$data,TRUE);
   echo $response;
}

, ajax

$('#button').click(function(){
var username = $('#username').val();

$.ajax({
   type:'POST',
   url:"<?php echo base_url(); ?>controller_name/get_view_ajax/",
   data: "username="+username,
   success:function(msg){
    $("#div_result").html(msg);
   },
   error: function(result)
   {
      $("#div_result").html("Error"); 
   },
   fail:(function(status) {
      $("#div_result").html("Fail");
   }),
   beforeSend:function(d){
    $('#div_result').html("<center><strong style='color:red'>Please Wait...<br><img height='25' width='120' src='<?php echo base_url();?>img/ajax-loader.gif' /></strong></center>");
   }

  }); 
});

<div id="div_result">
<a href="#" id="button">Click here </a>  

, (extra_info.php), get_view_ajax

<h1>This page is called from ajax function </h1>
+7

Keep in mind that dataType must be "HTML"

$.ajax({
        type: 'POST',
        url: '<?php echo base_url(); ?>/controllerName/functionName',
        dataType: "html",
        success: function (response) {
                   $("#oh").html(response);
                },
        error: function (error_) {
                    MYLOG(error_);
                }
       });
0
source

All Articles