I really have tried several ways to send post data via ajax. CSRF is blocking me and I dont want to disable or exclude the controller from CSRF check.
Way One:
Code:
function sendrow(e){
var working=$('#working');
var value="<?php echo $this->security->get_csrf_hash();?>";
var name="<?php echo $this->security->get_csrf_token_name(); ?>";
working.html('');
var path='http://localhost/ph/ctrl/process';
working.html('<img src="http://localhost/ph/assets/img/loading24.gif/>"').load(path, {name:value,'e':e}, function(data, status, response) {
working.html("");
if(status!='error'){
alert(true);
}
else {
alert('server error');
}
});
}
Method Two:
Code:
function sendrow(e){
$.ajax({
type: "post",
async: true,
url: path,
data: {name:value,'e':e},
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (msg) {
alert(true);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('server error');
}
});
}
I have tried opening form with open_form then serialize it in javascript but failed too. What am I missing exactly? The CSRF data (name and value) is set correctly. Every method I saw online didnt work too. If I change the method to "get" oriented method in my controller, it works fine but it must be posted.
Any clues?