Multiple JSON objects via AJAX

I used JSON.stringify but it returns undefined. Ajax call returns an array of objects e.g. {“FileName”: “example1.jpg”, “StatusCode”: 200, “StatusMessage”: “Succesfull”}, {“FileName”: “example2.jpg”, “StatusCode”: 200, “StatusMessage”: “Succesfull” }. How can I access to StatusMessage from the objects?

$.ajax({
url: my.webserviceUploadImageUrl,
type: 'POST',
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
data: formData,
success: function (result) {

var statusmessage = JSON.stringify(result[i]["StatusMessage"]);




var url = location.protocol + '//' + location.host + location.pathname;
if (url.indexOf('?') > -1) {
url += '&message=' + statusmessage;
} else {
url += '?message=' + statusmessage;
}


window.location.href = url;


},
error: function () {
self.statusMessage("Error uploading images. Please refresh the page and try again");
}
});

you need a dot between [i] and [“StatusMessage”]
eg. [i].["StatusMessage"]

I try with this but there appears an error -identifier expected

To be able to use i you need to use a for loop like this:

for (i = 0; i < myObj.cars.length; i++) {
}

Also, @Remiix is wrong and no dot is necessary or you could use this as an alternative to what you have now (without the dot)

result[i].StatusMessage

Hopes this helps! :slight_smile:

1 Like

There is no error now but it gives undefined for StatusMessage.I don’t know where is my problem, whatever I try every time is undefined.

Sorry, I forgot to mention that you need to change the myObj.cars.length with result.length
Hope this helps! :slight_smile: