Successfully executing an AJAX post, the function test
is utilized to process the passed data
.
$("#formID").submit(function (event) {
$('#postError').html('').hide();
$('#postInfo').html('loading results').show();
event.preventDefault();
$.ajax({
type: 'POST',
url: $("#formID").attr('action'),
data: $(this).serialize()
})
.done(function (data) {
$('#postInfo').html('').hide();
test(data);
})
.fail(function () {
$('#postInfo').html('').hide();
console.log(1);
});
})
However, an issue arises at this point.
function test(data) {
console.log(data);
if ($.isArray(data)){
$('#postSuccess').html(data).show();
}else {
$('#postError').html(data).show();
}
}
The output in the console.log is:
[{"a1":"1","a1amount":"300","a2":"2","a2amount":"300","a3":"3","a3amount":"300"
,"a4":"4","a4amount":"300","a5":"5","a5amount":"60"},
{"b1":"6","b1amount":"75","b2":"7","b2amount":"75","b3":"8","b3amount":"75"},
{"c1":"9","c1amount":"40","c2":"10","c2amount":"40","c3":"11","c3amount":"40"," c4":"12","c4amount":"40"}]
This seems like a standard JSON array. If I am correct, why does it trigger the else part? And if my assumption is incorrect, what could be causing the issue with the function or array?