简体   繁体   English

jQuery Ajax无法发布数据

[英]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. 在我的Codeigniter视图中,我有以下代码通过jquery ajax函数将当前密码和新密码发送到控制器,但是似乎数据没有传递到控制器。 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 试试这个通行证作为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!");
            }
        }
    });

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. 请检查CSRF的配置(如果已打开)以带来“ csrf_token_name”参数。

config/config.php : 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: 正确的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!");
            }
        }
    });

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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