简体   繁体   English

CodeIgniter AJAX:POST无法正常工作,GET正常运行

[英]CodeIgniter AJAX: POST not working, GET works fine

Fiddling inside CodeIgniter and trying to get a grip on it all as I've never worked with AJAX before. 摆弄CodeIgniter并尝试掌握所有内容,因为我以前从未使用过AJAX。

For some reason, my AJAX is working perfectly when I use the GET method, but if I switch it over to the POST method, it stops working. 由于某种原因,当我使用GET方法时,我的AJAX可以正常工作,但是如果我将其切换到POST方法,它将停止工作。

My JS: 我的JS:

$(document).ready(function(){

    $('.love').click(function(event) {

        $.ajax({

            type: 'GET',
            url: base_url + '/ajax/love_forum_post',
            data: { post_id: 2, user_id: 1, ajax: 1 },

        });

        return false;

    });

});

And my CONTROLLER: 而我的控制器:

function love_forum_post()

{

    $post_id = $this->input->get('post_id');
    $user_id = $this->input->get('user_id');
    $is_ajax = $this->input->get('ajax');

    if ($is_ajax)

    {

        $this->load->model('forums_model');
        $this->forums_model->add_love($post_id, $user_id);

    }

    // If someone tries to access the AJAX function directly.

    else

    {

        redirect('', 'location');

    }

}

If I switch the type to 'POST' inside my JS and then catch it on the other end with $this->input->post() it doesn't work. 如果我在JS内将类型切换为“ POST”,然后在另一端使用$ this-> input-> post()捕获它,则它将不起作用。

Any suggestions? 有什么建议么?

I have tested your code in 2 scenarios: first - without csrf protection, and I see no reason for your code not to run properly. 我已经在两种情况下测试了您的代码:首先-没有csrf保护,并且我认为没有理由让您的代码无法正常运行。 To be able to test it easier, append $.ajax call with success response. 为了能够更轻松地进行测试,请在$ .ajax调用后附加成功响应。 Something like this 像这样

success: function(response) {
            alert(response);
            }

And add response to your love_forum_post method. 并添加对您的love_forum_post方法的响应。

echo print_r($this->input->post(), true);

This would give you clean view of what it going on in your method. 这将使您清楚地了解方法中的情况。

In my installation everything works just fine. 在我的安装中,一切正常。

Second scenario is with csrf protection. 第二种情况是使用csrf保护。 In this case add new param to your post object. 在这种情况下,向您的post对象添加新的参数。

<?php if ($this->config->item('csrf_protection') === true) : ?>
            post_data.<?php echo $this->security->get_csrf_token_name()?> = '<?php echo $this->security->get_csrf_hash()?>'; 
<?php endif ?>

This would make CI accept post from this url. 这将使CI接受来自该URL的帖子。

Hopefuly it would help. 希望它将有所帮助。 Cheers 干杯

By any chance you have csrf_protection enabled? 您是否有机会启用了csrf_protection?

If yes you need to send the token and the value key as post parameter along with post request. 如果是,则需要将令牌和值键作为发布参数与发布请求一起发送。

Try to use this 尝试使用这个

$post_data = $_POST;

and print the post data using this 并使用此打印帖子数据

print_r($post_data);die();

and you can see there if you catch the post data; 并且您可以在那里查看是否捕获了帖子数据;

Gudluck!! 古德鲁克!

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

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