简体   繁体   中英

Can't get value of POST via ajax

I'm searching for about 4 hours and I still don't understand, why this code doesn't work.

I am using Codeigniter framework, csrf is off, xss filtering is off.

My js:

var url_to_ajax = base_url + "ajaxcalls/newcomment";
$.ajax({
        url: url_to_ajax,
        type: "POST",
        data: {parent_id: "a"},
        success: function ( data ) {
            alert(data);
        }
});

and in controller:

public function newcomment() {

        $parent_id = $this->input->post('parent_id');

        echo "please print it: ".$parent_id;
        print_r($_POST);
}

$parent_id is empty and $_POST is an empty array.. In the alert I see "please print it: Array ( )" and that's all.

Does someone know why I can't get that "a" in alert from controller?

Your code is working fine for me. May be there is a mistake in URL. Try this code and see if any error message comes.

var url_to_ajax = base_url + "ajaxcalls/newcomment";
$.ajax({
        url: url_to_ajax,
        type: "POST",
        data: {parent_id: "a"},
        success: function ( data ) {
            alert(data);
        },
        error: function (jqXHR, textStatus, errorThrown)
                        {
                            alert(errorThrown);
                        }
});

IF you are not using url rewrite, use "index.php" with base_url. Try this:

$.ajax({
    url: base_url + "index.php/ajaxcalls/newcomment",
    type: "POST",
    data: {parent_id: "a"},
    success: function ( data ) {
        alert(data);
    }
});

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