jquery and ajax in codeigniter


jquery and ajax in codeigniter

我只想在点击代码点火器中的按钮时显示数据库中的内容

这是我的脚本

function check()
{
    var base_url="<?php echo base_url();?>"; 
    $.ajax({
    url :  base_url+"/seller/getSubCategory", success : function(data)
    {$('#content').html(data);}});
}

在controller.php 中

public function getSubCategory()
    {
        $data['result']=$this->propertydetails->getPropertyCategories();
        $this->load->view('seller/postdetails',$data);
    }

这是型号.php

<?php
class propertydetails extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }   
    function getPropertyCategories()
    {
        $query=$this->db->get('orx_categories');
        if($query->result()>0)
        {
            return $query->result();
        }
        else 
        {
            return $query->result();
        }   
    }
}
?>

当我运行这段代码时,什么都没有发生,它不会显示任何错误,也不会返回任何信息。帮我解决这个问题?

我发现Charles Proxy在调试ajax请求时非常宝贵:http://www.charlesproxy.com/我还要仔细检查url帮助程序是否已加载。

这里有一个你可以尝试的通用示例,假设你有一个表单,并且你想使用该帖子数据,或者你可以做一些类似(".button"(的事情。点击(…

从查询中提取数据后,您可以将视图加载到变量中,而不是通过将视图加载程序的第三个参数设置为true来直接将其发送到输出中。通过这样做,您可以将多个视图发送回页面,从而避免了强制您在控制器中编写html。

Javascript代码

<!-- language: lang-js -->
$(document).ready(function(){
    $("#form").submit(function(e){
        e.preventDefault();
        $.post("base_url+"/controller/method", $("#form").serialize(),
        function(data){
        if(data.status == "valid"){
            $(".view1").html(data.view1);
            $(".view2").html(data.view2);
        }
        }, "json");
    });
});

和你的php控制器

class Controller extends CI_Controller {
public function method()
   {
      $this->load->model('Some_model','', TRUE);
      $data['results'] = $this->Some_model->some_function();
      if($data['results']['status'] == "valid"){
         $json['status'] = "valid";
         $json['view1'] = $this->load->view('view1',$data,true);
         $json['view2'] = $this->load->view('view2',$data,true);
         echo json_encode($json);
      }
      else {
         $json['status'] = "invalid";
         $json['error'] = "<h3>Login Failed</h3>";
         echo json_encode($json);
      }
   }
}

和您的php模型

class Some_model extends CI_Model {
public function some_function()
   {
      $query=$this->db->get('orx_categories');
      if($query->num_rows() > 0) {
         $sata['status'] = "valid";
         foreach($sql->result() as $row) {
            $data['results'][] = $row;
         }   
      }
      else {
         $data['status'] = "invalid";
      }
      return $data;
   }
}

在加载视图时,在控制器函数中传递第三个布尔参数true。这意味着ok获取该视图并将其存储在一个变量中,而不是渲染它,然后回显该变量值。参见下面的控制器功能代码

public function getSubCategory()
{
    $data['result']=$this->propertydetails->getPropertyCategories();
    //note the third parameter to the view method
    //it means get the content of the seller/postdetails view and store it
    //in a local variable $output
    $output = $this->load->view('seller/postdetails',$data,true);
   //now echo the $output so that the javascript function can consume
   echo $output;
}

将脚本更改为

function check() {
     var base_url="<?php echo base_url();?>"; 
     $.ajax({
          url : "<?php echo base_url();?>/seller/getSubCategory", 
          success : function(data){
                  $('#content').html(data);
          }
     });
 }

将控制器更改为:

public function getSubCategory()
    {
        $data = $this->propertydetails->getPropertyCategories();
        echo $data;
    }