Reputation:
Hi guys i am trying to insert form values in db using jquery ajax..while inserting i am getting error as CSRF Token mismatch. Tried inserting csrf token from headers.but still it is showing same error.
Here is my code:
function saveFile()
{
chk_EnumValuesValidation = chkEnumValuesValidation(isSoltion, stepCount);
if(!chk_EnumValuesValidation)
{
return false;
}
else
{
// Function to push "MainArray" in current Solution
pushVarMainArrayInThisSolution(isSoltion, var_main_arr.var_arr_values);
// alert('Aalaaaaaaa');
arr = ar;
var subject_list = $('#qtype_subject_id').val();
var ddl_topic_type = $('#qtype_topic_id').val();
var qtype_name = $('#qtype_name').val();
var qtype_option = $('#qtype_option').val();
var batch = [];
$(".batch:checked").each(function ()
{
batch.push(parseInt($(this).val()));
});
var sendInfo = {
'arr':arr,
'saveFile':1,
'subject_list':subject_list,
'topic_list':ddl_topic_type,
'qtype_name':qtype_name,
'qtype_option': qtype_option
};
$.ajax({
url: "/eqtype-editor/store",
type: "POST",
data :sendInfo,
contentType: "application/x-www-form-urlencoded",
success: function(response)
{
data = JSON.parse(response);
if(data.Success == 'Success')
{
// for same file creation when again click on save button
alert('Your file is saved!');
// window.location.href="qtype_list.php?pag=Masters";
}
else
{
alert(data.resp);
}
},
error: function (request, status, error)
{},
complete: function()
{}
});
}
}
Can anyone help me where i did mistake.Thank you in advance
Upvotes: 0
Views: 129
Reputation: 1302
Add this in Your HTML Header Section
<meta name="csrf-token" content="{{ csrf_token() }}" />
Your Ajax POST Function should be
$.ajax({
url: "/eqtype-editor/store",
type: "POST",
data :sendInfo,
headers : {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
contentType: "application/x-www-form-urlencoded",
success: function(response){
handle Success here.....
}
});
Upvotes: 1