Click here to Skip to main content
14,882,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
​Ajax call returns an array of objects -
This is an example of result output.

{"FileName":"example.jpg","Statuses":null,"ProductMatchedByCode":{"ProductID":"00000000-0000-0000-0000-000000000000","ProductDetailID":"00000000-0000-0000-0000-0","Code":"","Title":"","ProductImages":[{"Path:"\\v4\\images\example.jpg","Date":null,"OriginalFileName":"example.jpg","FileSize":null}],"IsSuccesfull":false,"StatusCode":200,"StatusMessage":""},"IsSuccessful":true,"StatusCode":200,"StatusMessage":'example.jpg' Uploaded Successful"}
{"FileName":"example2.jpg","Statuses":null,"ProductMatchedByCode":{"ProductID":"00000000-0000-0000-0000-000000000000","ProductDetailID":"00000000-0000-0000-0000-0","Code":"","Title":"","ProductImages":[{"Path:"\\v4\\images\example2.jpg","Date":null,"OriginalFileName":"example2.jpg","FileSize":null}],"IsSuccesfull":false,"StatusCode":200,"StatusMessage":""},"IsSuccessful":true,"StatusCode":200,"StatusMessage":'example2.jpg' Uploaded Successful"}


But Status Message was undefined, also I think I used the wrong way for result.length it doesn't give real objects length, it gives me something bigger ex.525.Any idea how can I fix it?


What I have tried:

$.ajax({
                url: self.webserviceUploadImageUrl,
                type: 'POST',
              
                processData: false,
                contentType: false,
                cache: false,
                data: formData,
                success: function (result) {
                
                   

                    for (var i = 0; i < result.length;i++) {
                    var statusmsg = JSON.stringify(result[0]).StatusMessage;

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

                    window.location.href = url;
                 
              
                },
                error: function () {
                    self.statusMessage("Error uploading product images. Please refresh the page and try again");
                }
                });
Posted
Updated 28-Feb-21 3:09am
Comments
oLiontas 28-Feb-21 12:33pm
   
Why don't you use json.parse() method to the result?
Member 13227309 28-Feb-21 12:43pm
   
I try with json.parse but there an error: Uncaught SyntaxError: Unexpected end of JSON input
oLiontas 28-Feb-21 13:17pm
   
Try to fix the json format as it the solution below shows and then use json.parse()

1 solution

Your JSON is malformed:
[{"Path:"\\v4\\images\example.jpg",

should be
[{"Path":"\\v4\\images\example.jpg",
       ^

Also
"StatusMessage":'example.jpg' Uploaded Successful"}

Should be
"StatusMessage":"'example.jpg' Uploaded Successful"}
                ^
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900