I'm having problem how to solve ajax post in codeigniter. GET method is working well but when I changed it to post, it is no longer working. I observed that when I disabled my csrf_protection in config file, it is working, but when I enabled it, it is NOT WORKING. I want to use csrf_protection enabled while post method is working. Can anybody help me? Thanks..
<script>
$(document).ready(function(){
$('#submit').click(function(event) {
$.ajax({
type: 'POST',
url: '/common/test',
data: {
<?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 ?>
},
success: function(response) {
alert(response);
}
});
return false;
});
});
</script>
<input type="text" name="name" id="name">
<input type="submit" value="submit" name="submit" id="submit">
On my Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
function test(){
echo 'test';
}
}