简体   繁体   English

在ajax调用中从服务器接收HTML并使用该HTML加载页面

[英]receive HTML from server in ajax call and load page with that HTML

I am working on CodeIgniter and AJAX . 我正在研究CodeIgniterAJAX What I am trying to do that I have a datatable which contains list of employee. 我要做什么,我有一个包含员工列表的数据表。 On button click I get the ID of employee and send it to my controller in which I get all the necessary details from DB of that specific employee. 在按钮上单击,我获取员工的ID ,然后将其发送到我的控制器,在控制器中,我从该特定员工的DB获取所有必要的详细信息。 I have already done this. 我已经做到了。 The issue I am facing is that how can I redirect to viewEmployeeProfile page after successful ajax call with that specific Employee Detail. 我面临的问题是,在使用该特定的“员工详细信息”成功进行ajax调用后,如何重定向到viewEmployeeProfile页面。

Here is my code: 这是我的代码:

JS JS

$('#viewAllEmployeeTable tbody').on('click', '.viewEmployeeDetail', function() {
    var data = viewAllEmployeeTable.row($(this).parents('tr')).data();
    employeeID = data.employeeID;
    $.ajax({
        url: "/ackamarackus/employee/viewEmployeeProfile",
        type: "POST",
        data: {
            "employeeID": employeeID
        },
        dataType: "json",
        success: function(data) {
            console.log(data);
        },
        error: function(error) {
            //console.log(error);
        }
    });
});

viewEmployeeProfile Function viewEmployeeProfile函数

public function viewEmployeeProfile() {
    $employeeID =  $this->input->post('employeeID');
    $data['employeeBasicDetails'] = $this -> getemployeeBasicDetailsFromDatabase($employeeID);
    $data['employeeDepartmentalDeails'] = $this -> getemployeeDepartmentalDeailsFromDatabase($employeeID);
    $data['employeeSalaryDetails'] = $this -> getemployeeSalaryDetailsFromDatabase($employeeID);
    $data['employeeEducationDetails'] = $this -> getemployeeEducationDetailsFromDatabase($employeeID);
    $data['employeeJobHistoryDetails'] = $this -> getemployeeJobHistoryDetailsFromDatabase($employeeID);
    $data['employeeTrainingDetails'] = $this -> getemployeeTrainingDetailsFromDatabase($employeeID);
    $data['header'] = 'template/header';
    $data['sidebar'] = 'template/sidebar';
    $data['main_content'] = 'viewEmployeeProfile';
    $data['footer'] = 'template/footer';

    echo $this -> load -> view('template/template', $data, TRUE);
}

template/template view 模板/模板视图

//This view is generic and implements templating 
$this->load->view($header);
$this->load->view($sidebar);
$this->load->view($main_content);
$this->load->view($footer);

after the request console.log(data) ; 在请求console.log(data) gives me the HTML of viewEmployeeProfile view I have also tried to echo the content from $data array like print_r($employeeBasicDetails); 给了我viewEmployeeProfile view的HTML,我也试图从$ data数组中回显内容,例如print_r($employeeBasicDetails); and it gives me the correct information. 它给了我正确的信息。 BUT I don't know how to load that view with the HTML received from ajax call ? 但是我不知道如何使用从ajax调用接收的HTML加载该视图?

Any idea how to do this or is there any good hack for this solution, may be I am doing it all wrong. 任何想法如何做到这一点,或者此解决方案有什么好的技巧,可能是我做错了。 Any input in this will be highly appreciated. 对此的任何投入将不胜感激。 Thanks. 谢谢。

This line of your ajax call specifies the type of data to expect in return from the server. ajax调用的这一行指定了从服务器返回的期望数据类型。 If you are outputting HTML you need to change it as currently you are telling the ajax request to expect json in return 如果您正在输出HTML,则需要更改它,因为当前您正在告诉ajax请求期望json作为回报

dataType: "json"

To insert it into your page you just need to specify where you want it. 要将其插入页面,只需指定所需位置即可。 Create an empty span/div whatever and give it an id, then in your success callback you can insert the returned data into that element on your page. 创建一个空的span / div并为其指定一个ID,然后在成功回调中,您可以将返回的数据插入页面上的该元素。 Say your div id was employee then you add the following to your success callback: 假设您的div ID是雇员,然后将以下内容添加到成功回调中:

$('#employee').html(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM