After submitting my form (with jQuery Ajax request), I get an array back in the following structure (copied from the console), when i expand the first object:
[Object, Object, Object]
0: Object
msg0: "ok"
I need to be able to refer to msg0
(and msg1
, msg2
etc.) in the array - with JS/jQuery - and the corresponding message (e.g. "ok"
) for a conditional statement.
for example:
if (msg1 === "ok") {
// do stuff
}
How can I get to those values? Any help much appreciated! This is what code I have so far:
var someData = $('#someForm').serialize(); // form with data
var btnAjaxSubmit = $('.btnAjaxSubmit'); // button (type: button) to submit data
$('.btnAjaxSubmit').click(function() {
var request = $.ajax({
url: "https://somesite.com/data.asp",
method: "POST",
data: someData
});
// getting response
request.done(function(status) {
console.log(status['response']);
// response ok
if (status['response'] === "ok") {
alert('status is "updated"')
// session timeout
} else if (status['response'] === "timeout") {
alert('log in again.');
// all other cases
} else {
alert('this is the other case');
}
});
// no response at all
request.fail(function(status) {
alert('request failed!');
});
});