datatype doesn't need to be json for a post, just post it through the jquery ajax normal, json is more for getting a response back to a jquery request. Here a laravel controller method is taking care of business.
Jan 23, 2017
21
Level 15
Need some advice on saving a Ajax form data into L5
I'm making a mistake somewhere but I can't see it. Essentially my Ajax function works. But I can't get it into my controller for saving into my DB. What am I doing wrong ?
Many Thanks !
$("#editsubmit").click(function () {
event.preventDefault();
var formData = $("form").serialize();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: "POST",
url: 'save_edit',
dataType: 'json',
data: formData,
success: function () {
console.log(formData); // GETTING A RESULT HERE.
localStorage['alert'] = 'true';
location.href = "http://auburntree/open_quotes"
},
error: function () {
alert('there has been a system level error - please contact support')
}
});
});
Controller ( Abreviated) :
public function save_quote_edited(Request $request){
$newQuotation = new Quotation();
$newQuotation->discount = $request->get('discount');
$newQuotation->discount_value = round($request->get('discountvalue_hidden',2));
$newQuotation->netofdiscount = $request->get('netofdiscount_hidden');
$newQuotation->gst = $request->get('gst_hidden');
$newQuotation->total = round($request->get('final_total_hidden',2));
$newQuotation->save();
}
Please or to participate in this conversation.