如何从codeigniter视图发出ajax请求

时间:2013-02-26 06:07:27

标签: php jquery html ajax

我有一个观点

     <script type="text/javascript">

function ajax_articles() {

    $.ajax({
      url: "http://localhost/codeigniter/CodeIgniter_2.1.3/index.php/patientmain/search_doctor_by_name/"+$('#search')[0],
      async: false,
      type: "POST",
      data: "type=article",
      dataType: "html",
      success: function(data) {
        $('#ajax').html(data);
      }

  });

}
</script>
<div class="content">
<div class="content-left">
<div  class="row1">
   <h2>Welcome <? echo $username ?></h2>
   <form name="search">
   Search Doctor by name : <input name="name" id="search" type="text" onChange="ajax_articles();">
   </form>
   </div>

   <div id="ajax">


</div>
</div>

<div class="content-right">
<div class="mainmenu">
<h2 class="sidebar1">My Menu</h2>
<p><ul>
  <li><a href="#">this is a dummy link 1</a></li>
  <li><a href="#">this is a dummy link 2</a></li>
  <li><a href="#">this is a dummy link 3</a></li>
  <li><a href="#">this is a dummy link 4</a></li>
  <li><a href="#">this is a dummy link 5</a></li>
  <li><a href="#">this is a dummy link 6</a></li>
  <li><a href="#">this is a dummy link 7</a></li>
  <li><a href="#">this is a dummy link 8</a></li>
  <li><a href="#">this is a dummy link 9</a></li>
  <li><a href="#">this is a dummy link 10</a></li>
</ul></p>
</div>


</div>
</div>

这是我的观点,现在我想通过这个jquery ajax调用http://localhost/codeigniter/CodeIgniter_2.1.3/index.php/patientmain/search_doctor_by_name。但什么都没发生。没有回应。我认为代码中存在一些问题,任何人都可以指出问题所在。请建议。 感谢

3 个答案:

答案 0 :(得分:0)

像这样:

public function search_doctor_by_name($search_name = '') {
      //$this->db->where('name',$search_name);
      //$row = $this->db->get('table')->result_array();
      $this->output->set_output($data);//or echo 'some data';
}

可能你需要萤火虫。^ _ ^

答案 1 :(得分:0)

function ajax_articles() {
$.post("http://localhost/codeigniter/CodeIgniter_2.1.3/index.php/patientmain/search_doctor_by_name/"+$('#search').val(),{},function(data) {$('#ajax').html(data);});

}

试试这个

答案 2 :(得分:0)

包括

<script src="http://code.jquery.com/jquery-latest.js"></script>

并使用此功能

$(document).ready(function(){
    $("#search").change(function(){
        dataString = $("#JqAjaxForm").serialize();
        $.ajax({
        type: "POST",
        url: "your ci url",
        data: dataString,
        dataType: "json",
        success: function(data) {
            //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this

            alert(data);// this will be a json string

        }

        });         

    });
});

并按照这样的文本框

<input id="search" type="text" name="name_ajax" />

这是您的表单ID

JqAjaxForm