I have a modal on my page that submits some data via ajax to my Laravel application, which is then validated to make sure a field is present.
If the field isn't present then Laravel will return a json response with an error status.
I am then checking for the status in the success part of my ajax call for this and displaying an error message if this is the case.
However, even though I am returning an error status, it seems my if statement is missing this and continuing on with the statement.
Below is the whole script:
<script>
$(document).ready(function () {
$("#addShiftButton").click(function(){
$.ajax({
type: "POST",
url: "calendar/addhours",
data: {
'name':$('#name').val(),
'start_time':$('input[name=start_time]').val(),
'finish_time':$('input[name=finish_time]').val(),
'add_shift_date':$('#add_shift_date').val(),
'breaks':$('input[name=breaks]').val(),
'shift_status':$('#shift_status').val()
},
success: function(data){
if (data.status == "error")
{
$("#error").show();
}
else
{
$('#addShiftModal').modal('hide'); //hide popup
//location.reload();
}
},
error: function(){
alert("Error");
}
});
});
});
</script>
I have checked the call and it is being successfully made and a json response is received like so: {"status":"error"}
.
However the else
part of my if
statement is being executed. Can anyone see why?