dqyl2374 2014-04-01 10:55
浏览 60
已采纳

使用CodeIgniter进行Ajax查询?

I am currently developing a website and was using strictly PHP, after a few hours of development I am noticing I really need to perform AJAX queries. However I am using CI and slightly confused on how I call Ajax queries - especially to a controllers method. I am looking to post data using the query,I have found the following using Jquery:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

but if I had a controller called main and method called postBack() - how would I pass postback() the data?

Could anyone point me in the right direction it would be much appreciated - apologies if this is extremely simple to say I am a newbie is an understatement.

  • 写回答

3条回答 默认 最新

  • douzhi2760 2014-04-01 12:42
    关注

    Here is a Codeingiter/Jquery function pair that I wrote not long ago:

    Javascript:

    var BASE_URL = <?php echo base_url(); ?>
    var stuff = "";
    
     $('div.get_this').each(function(){
        if(stuff ){
            stuff = stuff + '-';
        }
        stuff = stuff + $(this).html();
    });
    
    $.ajax({
      type: "POST",
      url: BASE_URL+"gate",
      data: {'data':stuff},
      success: function(data){
        ...
      });
    });
    

    Codeigniter Controller:

    class Gate extends CI_Controller {
    
        public function index(){
    
            if(isset($_POST['data'])){
    
                $data = explode('-', $_POST['data']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题