在ajax调用之后加载视图,CodeIgniter

时间:2022-10-08 09:16:20

I have an ajax call from javascript to a method in a controller, the method in the controller should be loading a view(like, show a new page on the screen), Although everything seems ok, the new view doesn't seem to load on the screen. I cannot use header or windows.location because, i am passing an array variable, containing data to be used in the view.

我有一个从javascript到控制器中的方法的ajax调用,控制器中的方法应该加载一个视图(比如,在屏幕上显示一个新页面),虽然一切似乎都没问题,新视图似乎没有加载屏幕上。我不能使用header或windows.location,因为我传递的数组变量包含要在视图中使用的数据。

The page is visible under the Network tab (preview sub tab, while selecting the ajax) of the Chrome debugging console. But the main screen stays as it is.

该页面在Chrome调试控制台的“网络”选项卡(预览子选项卡,同时选择ajax)下可见。但主屏幕保持不变。

If somebody has faced a similar issue, or has a solution, please help me.

如果有人遇到类似的问题,或者有解决方案,请帮助我。

Thanks!!

谢谢!!

3 个解决方案

#1


9  

Ok here is what you are doing wrong.

好的,这就是你做错了。

when you request the page using ajax it does not return you that page.

当您使用ajax请求页面时,它不会返回该页面。

when you use $this->load->view('pagename',$datapassed); it loads the view and thats why you see nothing.

当你使用$ this-> load-> view('pagename',$ datapassed);它加载视图,这就是为什么你什么也看不见。

What you have to do is use

你要做的就是使用

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

what it will do is it will return that page and save it in $data after that you can print it using

它会做什么,它将返回该页面并将其保存在$ data中,之后您可以使用它来打印它

$this->set_output($data); 

and receive this in ajax result and load it in a div.

并在ajax结果中接收它并将其加载到div中。

and if you want to refresh the whole page you can use

如果你想刷新整个页面,你可以使用

$(body).html(result);

You have to understand that you need to send the html page by supplying that third parameter in load view.

您必须了解您需要通过在加载视图中提供第三个参数来发送html页面。

#2


4  

In controller class

在控制器类中

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

In view file where you initiate ajax call

在视图文件中,您启动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>  

Another view file to be loaded on controller function (extra_info.php) as refered on get_view_ajax function

另一个要在控制器函数(extra_info.php)上加载的视图文件,它引用了get_view_ajax函数

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

#3


0  

Keep in mind dataType must be "html"

请记住dataType必须是“html”

$.ajax({ type: 'POST', url: '/controllerName/functionName', dataType: "html", success: function (response) { $("#oh").html(response); }, error: function (error_) { MYLOG(error_); } });

$ .ajax({type:'POST',url:'/ controllerName / functionName',dataType:“html”,success:function(response){$(“#oh”)。html(response);},error: function(error_){MYLOG(error_);}});

#1


9  

Ok here is what you are doing wrong.

好的,这就是你做错了。

when you request the page using ajax it does not return you that page.

当您使用ajax请求页面时,它不会返回该页面。

when you use $this->load->view('pagename',$datapassed); it loads the view and thats why you see nothing.

当你使用$ this-> load-> view('pagename',$ datapassed);它加载视图,这就是为什么你什么也看不见。

What you have to do is use

你要做的就是使用

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

what it will do is it will return that page and save it in $data after that you can print it using

它会做什么,它将返回该页面并将其保存在$ data中,之后您可以使用它来打印它

$this->set_output($data); 

and receive this in ajax result and load it in a div.

并在ajax结果中接收它并将其加载到div中。

and if you want to refresh the whole page you can use

如果你想刷新整个页面,你可以使用

$(body).html(result);

You have to understand that you need to send the html page by supplying that third parameter in load view.

您必须了解您需要通过在加载视图中提供第三个参数来发送html页面。

#2


4  

In controller class

在控制器类中

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

In view file where you initiate ajax call

在视图文件中,您启动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>  

Another view file to be loaded on controller function (extra_info.php) as refered on get_view_ajax function

另一个要在控制器函数(extra_info.php)上加载的视图文件,它引用了get_view_ajax函数

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

#3


0  

Keep in mind dataType must be "html"

请记住dataType必须是“html”

$.ajax({ type: 'POST', url: '/controllerName/functionName', dataType: "html", success: function (response) { $("#oh").html(response); }, error: function (error_) { MYLOG(error_); } });

$ .ajax({type:'POST',url:'/ controllerName / functionName',dataType:“html”,success:function(response){$(“#oh”)。html(response);},error: function(error_){MYLOG(error_);}});