I am working on this laravel 5 app and I can't seem to figure out why my Ajax call is not working for this particular form because it has been working well for other forms. Sample code below.
{!! Form::model($training, ['url' => 'trainings/' . $training->id,
'method' => 'PUT',
'class' => 'form-horizontal',
'id' => 'edit_training_form',
'role' => 'form']) !!}
// Form body
{{ Form::close() }}
Ajax Call Sample code below:
$("document").ready(function() {
$("#edit_academic_form").on('submit', updateAcademic);
$("#edit_training_form").on('submit', updateTraining);
}
function ajaxCall(context) {
$.ajax({
type: "PUT",
data: context.serialize(),
url: context.attr('action'),
dataType: 'json',
cache: false,
beforeSend: function() {
$(".validation-errors").hide().empty();
$(".success-message").hide().empty();
},
success: function(data){
$(".success-message").append(data.message).show();
$(".success-message").delay( 2000 ).fadeOut();
},
error: function(data){
var errors = data.responseJSON;
console.log(errors);
// Render errors with JS
$.each(errors, function(index, value)
{
if (value.length != 0)
{
$(".validation-errors").append('<li>'+ value +'</li>');
}
});
$(".validation-errors").show();
}
});
}
function updateTraining(e){
e.preventDefault();
ajaxCall($(this))
}
function updateAcademic(e){
e.preventDefault();
ajaxCall($(this))
}
The call for updateAcademic is working pretty well but updateTraining is not working. Firebug does not show any error. It's kind of doing some direct form submit. I have cleared browser cache to make sure the js is being read, change browser but no result. Looking at the source of the code from my browser the form is well defined (id is ok) as shown below:
<form method="POST" action="http://localhost:8000/trainings/1" accept-charset="UTF-8"
class="form-horizontal" id="edit_training_form" role="form">
<input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="QvvgMIWZN1VYGUOF2zGE8ALgVnAYaZUS5SseW4i5">
</form>
Appreciate help
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire