【问题标题】:CodeIgniter, Ajax call does not get into ControllerCodeIgniter,Ajax 调用不进入控制器
【发布时间】:2013-03-11 08:41:34
【问题描述】:

在查看ci-ajax-csrf-problem 上的解决方案后,我在脚本中添加了以下行,它工作正常。

var post_data = {
    '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}

插入

$.ajax({
        url: '<?php echo base_url()."ajax/test";?>',
        type:'POST',
        dataType: 'json',
        data: post_data, 

谢谢大家的帮助:)

我是 Ajax/Jquery 的新手,我正在按照 jorge torres 的 CodeIgniter 的 Ajax 指南在我的网站上实现一个简单的 ajax 调用,但遇到了问题。

我创建了一个控制器 Ajax,这是代码 sn-p。

class Ajax extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function test() {
        $output_string = 'This is a test';  
        echo json_encode($output_string);
    }

    public function test2(){
        $this->load->view('test.php');
    }
}

这是该控制器的视图,它与教程中的视图相同,除了我添加了加载 url 帮助器 $this->load->helper('url');在第一行

这是脚本代码的 sn-p。

#getdata 是按钮类型,#result_table 是 div

$('#getdata').click(function(){
$.ajax({
        url: '<?php echo base_url().'ajax/test';?>',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call
});

我可以成功访问 localhost.com/codeigniter/ajax/test2,但是当我点击按钮时,什么也没有发生。

我试过查看页面源信息,url是正确的

$.ajax({
        url: 'http://localhost/codeigniter/ajax/test',
        type:'POST'
        ....

也可以直接访问localhost/codeigniter/ajax/test,并显示输出信息。

我正在使用 CodeIgniter 2.1.3,我的 localhost 在 XAMPP 1.7.3 上运行

提前谢谢你:)

【问题讨论】:

  • 检查您的浏览器控制台是否有任何错误
  • 你是否将 jquery 代码包装在 $(document).ready(function() { ... }); ..?
  • 我建议您安装 firebug 或某种开发工具。检查控制台是否有错误。
  • 请使用它的萤火虫工具进行测试。看看当你点击按钮时会发生什么。
  • 感谢您的回复@arun-p-johny,我尝试查看浏览器控制台,它返回 500 Internal Server Error 并显示错误消息“不允许执行您需要的操作”。

标签: php codeigniter jquery codeigniter-2


【解决方案1】:

在查看ci-ajax-csrf-problem 上的解决方案后,我在脚本中添加了以下行,它工作正常。

var post_data = {
    '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}

插入

$.ajax({
        url: '<?php echo base_url()."ajax/test";?>',
        type:'POST',
        dataType: 'json',
        data: post_data, 

谢谢大家的帮助:)

【讨论】:

    【解决方案2】:

    您的 .click() 事件处理程序是否按预期工作?

    $('#getdata').click(function(){
     alert('clicked');
     ........
    

    如果没有,请尝试按照@Sudhir 的建议包装 jquery 代码 in $(document).ready(function() { ... });

    $(document).ready( function() { 
          $('#getdata').click(function(){
          alert('clicked');
          ........
    
    });
    

    如果是,如果您还不知道,有一个名为firebug 的工具可以检查您的 AJAX 请求。 我认为您的 AJAX 网址没有问题。 到目前为止,这是我的答案。希望这也有帮助。

    【讨论】:

    • 感谢您的回复,我使用 firebug 检查了控制台,它返回 500 内部服务器错误并显示“不允许您请求的操作”的消息。
    【解决方案3】:

    您的 config.php 中是否启用了输出压缩(gzip)?如果您压缩输出,当您使用 echo 并返回 500 服务器错误时,它将失败。

    【讨论】:

    【解决方案4】:

    我认为您的 ajax 调用存在问题。我想应该是这样的:

    $(document).ready (function () { 
       $('#getdata').click(function(){
         $.ajax({
            url: '<?php echo base_url()."ajax/test";?>',
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                    $('#result_table').append(output_string);
                } // End of success function of ajax form
            }); // End of ajax call
        });
    });
    

    如果 firebug 控制台显示不允许执行您需要的操作,则可能是 CSRF 令牌问题,请在 codeigniter 配置中将其关闭。

    在配置文件中添加。

    $config['csrf_protection'] = FALSE; 
    

    希望这会有所帮助:)

    【讨论】:

    • 抱歉回复晚了,是的,是因为启用了 CSRF 造成的,我找到了一个解决方案,它工作正常 :) 谢谢你的帮助
    【解决方案5】:

    你试过了吗

    $(document).ready (function () { 
       $('#getdata').click(function(){
         $.ajax({
            url: '/ajax/test',
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                    $('#result_table').append(output_string);
                } // End of success function of ajax form
            }); // End of ajax call
        });
    });
    

    相对于&lt;?php base_url; ?&gt;

    您是否在 CI 配置中输入了您的网站网址?

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2014-06-01
      • 2011-06-29
      • 1970-01-01
      相关资源
      最近更新 更多