Jquery ajax无法发布数据

时间:2013-03-06 06:02:25

标签: php codeigniter jquery

在我的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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在$ .ajax中({url只传递页面的url &安培;使用url删除要传递的变量 添加数据 数据:{new_pass:new_pass_valu,cu_pass:cu_pass_value},

答案 2 :(得分: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);
}

答案 3 :(得分: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);
}