简体   繁体   中英

Jquery ajax unable to post data

Im my Codeigniter view, I have the following code to send the current and new passwords to the controller via jquery ajax function, but it seems like the data is not passed to the controller. See if you can help me with this.

 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!");
            }
        }
    });

Controller:

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

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

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

Try this pass post as json data

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!");
            }
        }
    });

Controller

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

please check the config of CSRF, if it is open, to bring 'csrf_token_name' param.

config/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;

correct ajax post request:

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!");
            }
        }
    });

Controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM