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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15164599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:41:52  来源:igfitidea点击:

loading a view after an ajax call, CodeIgniter

phpajaxcodeignitercontroller

提问by Archer

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!!

谢谢!!

回答by Muhammad Nasir

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 页面。

回答by Dipen

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>

回答by mudassar031

Keep in mind dataType must be "html"

请记住 dataType 必须是“html”

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