简体   繁体   中英

Unable to receive ajax request in codeIgniter PHP

I have just started codeigniter and I am stuck at sending employee ID to my controller.

Actually, I have a datatable which shows all the registered employees, and there is a button which get the ID of the employee of row clicked and send it to controller through ajax call but i am unable to receive it in my ajax call.

JS code

$('#viewAllEmployeeTable tbody').on('click', '.viewEmployeeDetail', function() {
    var data = viewAllEmployeeTable.row($(this).parents('tr')).data();
    console.log(data);
    employeeID = data.employeeID;
    alert(employeeID);
    $.ajax({
        url: "/ackamarackus/employee/viewEmployeeProfile",
        type: "GET",
        data: {
            "employeeID": employeeID
        },
        dataType: "json",
        success: function(data) {
            console.log(data);
        },
        error: function(error) {
            console.log(error);
        }
    });
});

Controller

public function viewEmployeeProfile() {
    $name =  $this->input->post('employeeID');
    echo "INPUT";
    echo $name;
    die();
}

This is what ajax is sending : employeeID:1000

Can anyone tell me what I am doing here? I have already tried google and stack overflow link, but nothing solved my problem. Thanks

您不能发送type: 'GET'字段data ,将其更改为type:'POST' ,这将解决您的问题:)

You are sending employeeID as GET method and accessing it as POST in Your controller, change your type in ajax request to POST as follows

$('#viewAllEmployeeTable tbody').on('click', '.viewEmployeeDetail', function() {
    var data = viewAllEmployeeTable.row($(this).parents('tr')).data();
    console.log(data);
    employeeID = data.employeeID;
    alert(employeeID);
    $.ajax({
        url: "/ackamarackus/employee/viewEmployeeProfile",
        type: "POST", //Your problem here
        data: {
            "employeeID": employeeID
        },
        dataType: "json",
        success: function(data) {
            console.log(data);
        },
        error: function(error) {
            console.log(error);
        }
    });
});

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