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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀