php - CodeIgniter,Ajax 调用未进入 Controller

标签 php codeigniter jquery codeigniter-2

查看 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 中的 Ajax for CodeIgniter 指南在我的网站上实现一个简单的 ajax 调用并遇到了问题。

我创建了一个 Ajax Controller ,这是代码片段。

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');
    }
}

这是该 Controller 的 View ,它与教程中的 View 相同,只是我添加了加载 url 帮助程序 $this->load->helper('url');在第一行

这是脚本代码的片段。

#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,但是当我单击按钮时,没有任何反应。

我尝试查看页面源信息,网址是正确的

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

直接访问localhost/codeigniter/ajax/test也是可以的,它会显示输出消息。

我正在使用 CodeIgniter 2.1.3,我的本地主机在 XAMPP 1.7.3 上运行

提前谢谢你:)

最佳答案

查看 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, 

谢谢大家的帮助:)

关于php - CodeIgniter,Ajax 调用未进入 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334244/

相关文章:

javascript - 如何自动缩放mPDF中的SVG对象以适合页面的其余部分?

javascript - 在action.php 的表单之外定位按钮?

javascript - 我想将日期格式更改为 "yyyy-mm-dd"

php - 将模型名称作为函数中的参数传递并使用其方法,如 "$this-> $model_name->method()"codeigniter

javascript - jsTree选择独立于层次结构的复选框

jquery - 使用 jquery 加载文件内容

javascript - 当数据表具有复选框选定行时如何启用/禁用按钮

php - 了解 WordPress Hook 的调用方式

php - PHP 类中的变量作用域

mysql - 如何使用mamp将数据库与codeigniter连接