0

在我的 Codeigniter 视图中,我有以下代码通过 jquery ajax 函数将当前密码和新密码发送到控制器,但似乎数据没有传递给控制器​​。看看你能不能帮我解决这个问题。

 var url = '<?php echo base_url();?>index.php/it_inventory/change_my_pass';
    $.ajax({
        type: "POST",
        url: url,
        //datatype: "json",
        data:'cu_pass=' + cu_pass + '&new_pass=' + new_pass,
        success: function(r){
            if(r==1){
                alert("Password Changed Successfully!");
            }else{
                alert("Error changing password!");
            }
        }
    });

控制器:

public function change_my_pass($cu_pass="", $new_pass=""){
    //$cu_pass = $this->input->post('cu_pass');
    //$new_pass = $this->input->post('new_pass');
    echo $this->it_inventory_model->change_my_pass($cu_pass, $new_pass);
}
4

4 回答 4

0

您可以使用 serialize() 方法获取表单数据并将其传递给 ajax 数据参数。

于 2013-03-06T06:04:05.427 回答
0

在 $.ajax({ url 仅传递页面的 url 并使用 url 删除要传递的变量添加数据数据:{new_pass: new_pass_valu, cu_pass: cu_pass_value},

于 2013-03-06T06:09:05.477 回答
0

尝试将此通行证作为 json 数据

var url = '<?php echo base_url();?>index.php/it_inventory/change_my_pass/';
    $.ajax({
        type: "POST",
        url: url,
        datatype: "json",
        data:{cu_pass: cu_pass,new_pass: new_pass},
        success: function(r){
            if(r==1){
                alert("Password Changed Successfully!");
            }else{
                alert("Error changing password!");
            }
        }
    });

控制器

public function change_my_pass($cu_pass="", $new_pass=""){
    $cu_pass = $this->input->post('cu_pass');
    $new_pass = $this->input->post('new_pass');
    echo $this->it_inventory_model->change_my_pass($cu_pass, $new_pass);
}
于 2013-03-06T06:13:55.050 回答
0

请检查 CSRF 的配置,如果它是打开的,带上 'csrf_token_name' 参数。

配置/config.php

/*
|--------------------------------------------------------------------------
| Cross Site Request Forgery
|--------------------------------------------------------------------------
| Enables a CSRF cookie token to be set. When set to TRUE, token will be
| checked on a submitted form. If you are accepting user data, it is strongly
| recommended CSRF protection be enabled.
|
| 'csrf_token_name' = The token name
| 'csrf_cookie_name' = The cookie name
| 'csrf_expire' = The number in seconds the token should expire.
*/
$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;

正确的ajax发布请求:

var url = '<?php echo base_url();?>index.php/it_inventory/change_my_pass/';
    $.ajax({
        type: "POST",
        url: url,
        //datatype: "json",
        data:'cu_pass=' + cu_pass + '&new_pass=' + new_pass + '&<?php echo config_item('csrf_token_name');?>=<?php echo $this->input->cookie(config_item('csrf_cookie_name'))?>',
        success: function(r){
            if(r==1){
                alert("Password Changed Successfully!");
            }else{
                alert("Error changing password!");
            }
        }
    });

控制器:

public function change_my_pass(){
    $cu_pass = $this->input->post('cu_pass');
    $new_pass = $this->input->post('new_pass');
    echo $this->it_inventory_model->change_my_pass($cu_pass, $new_pass);
}
于 2013-03-06T06:25:09.373 回答