Ajax驱动的内容使用CodeIgniter

xan*_*xan 7 javascript php ajax jquery codeigniter

我正在制作一个网页,这是一个单页网站,通过CodeIgniter中的Ajax与服务器进行交互.一般编码具有以下类型:

controller (user.php):

public function get_user_content() {
    $id = $this->input->post('id');
    $hits = $this->user_model->user_data($id);
    $s = '';    
    foreach ($hits as $hit) {
       $s .= $hit->name;
       $s .= $hit->age;
    }
    echo $s;
}
Run Code Online (Sandbox Code Playgroud)

model(user_model.php):

function user_data($id) {
   //do sql operation
   return $query->result();
}
Run Code Online (Sandbox Code Playgroud)

视图:

...
...
<a href="#" id="23" class="user-data">Click here for user details</a>
...
...
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

('.user-data').click(get_user_data);
....
....
function get_user_data(response) {
    return $.ajax({
        type: "POST",
        url:  "<?php echo base_url();?>index.php/user/get_user_content",
        data: { id: this.id },
        success: function(response) {
            $("#somediv").append(response);
            $(".someclass").click(another_function);
        },
        error: function(error) {
            alert("Error");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,查看上面的javascript,对于将一些数据发送到服务器的所有操作都有单独的函数,并且通过更新特定的html内容Ajax.

我有以下问题(我只是对这个东西不熟悉):

1. Is there any better way of doing ajax in javascript than my implementation.
2. I'm not using the concept of views in CodeIgniter. I just `echo` results through my controller functions that gets embedded in javascript. This is because I want dynamic update in my app. It is a single page and there is no concept of new-page/new-tab. Is there any better way? 
Run Code Online (Sandbox Code Playgroud)

我不知道任何可能使其更容易/更优化的开源项目.

Rau*_*ria 0

第一个答案:

ajax 请求似乎很好,您也可以添加 dataType 选项来期望特定类型的响应,当您使用 post 时,您可以使用jquery.post作为替代方案

例子

$.post( "<?php echo base_url();?>index.php/user/get_user_content", function(data) {
  alert( "success" );
}, 'html') // here specify the datatype
.fail(function() {
  alert( "error" );
})
Run Code Online (Sandbox Code Playgroud)

您还可以使用完成回调而不是成功

第二个答案:

控制器

public function get_user_content() { 
    $id = $this->input->post('id');
    $hits = $this->user_model->user_data($id);
    $user_array = array();
    foreach ($hits as $hit) {
       $temp_array = array();
       $temp_array = array('name' => $hit->name);
       $temp_array = array('age' => $hit->age);  
       $user_array = array($temp_array);

    }
    $this->load->view('user', $user_array);
}
Run Code Online (Sandbox Code Playgroud)

莫代尔

保持不变

查看(用户.php)

例如说 user.php

<?php
echo "<div class='somediv'>";
if (sizeof($user_array)) {
  for ($row = 0; $row < sizeof($user_array); $row++ ) {
     echo "User Details: Name - " . $user_array[$row]['name'] . ", Age - " . $user_array[$row]['age']; 
     echo "<br/>";
  }      
} else { 
  <a href="#" id="23" class="user-data">Click here for user details</a>
} 
echo "</div>";
?>
Run Code Online (Sandbox Code Playgroud)

JavaScript

$('.user-data').on('click' function () {  // better to use event delegation as content is loaded dynamically
   get_user_data(); 
});

function get_user_data() {
    $.post( "<?php echo base_url();?>index.php/user/get_user_content", function(data) {
      alert( "success" );
      $("#somediv").append(data);
      $(".someclass").click(another_function);
    }, 'html') // here specify the datatype
    .fail(function() {
      alert( "error" );
    });
}
Run Code Online (Sandbox Code Playgroud)

参考

stackoverflow.com/questions/18471627/codeigniter-load-a-view-using-post-and-ajax