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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度