dongyong3590 2019-06-03 13:30
浏览 272

从jQuery调用Controller函数

I'm working on some codes. Here's my code

jQuery

duration = 30;
var countdown = setInterval(timer,1000);
var url = window.location.origin + '/pro/index.php/Test';

function timer(){
  duration = duration - 1; 
  if(duration<=0){
    clearInterval(countdown);
    window.location(url+'/next');
  }
}

I want to redirect and run function in Test/next. In next() method, it will rule where the link should go. url helper also has been added automatically in Loader

Controller

public $num;
public $val;

public function index(){
  // get the value from GET method
  $this->num = $this->input->get('num');
  $this->val = $this->input->get('val');
}

public function next(){
  $x = $this->num;
  $x = $x + 1;
  if($x < 4){
    redirect(site_url('index.php/Test?x='.$x/.'&num='.$this->num));
  }
  else{
    // it will redirect to another page
    // redirect(site_url('index.php/Home'));
    echo 'x = '.$x.', num = '.$this->num;
  }

(The codes above have been simplified).

The method has been run, but the url was localhost/pro/index.php/Test/next and echo-ing :

x = , num =

It seems like the global property doesn't work for the function called from JS/jQuery. Any explanation or solution for this?

  • 写回答

2条回答 默认 最新

  • dpzo13732 2019-06-03 13:38
    关注

    You can access controller function from view page to using base URL (Click here how to do it?). Then use jQuery Ajax call controller function.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    $.ajax({
        type: 'post', url: '<?php echo base_url("next");?>',
        data: {},
        success: function(result){
            console.log(result);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码