Convert string to array in ajax call in javascript -


i making ajax call , getting result array looks result = [one, two, three];

i need convert array , iterate it. since string has no quotes around values array isn't read correctly.

here code, if can please show me correct way it. thanks.

xmlhttp.open("post","searchservlet", true); xmlhttp.onreadystatechange =      function(){          if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {               resultdata = xmlhttp.responsetext;            // resultdata = [one, two, three];                // need make valid array iterate                (var = 0; < resultdata.length; i++) {                   console.log(resultdata[i]);               }         }     };   

hi "user" , welcome so. code sample , well-formatted question :)


since http requests use strings (they don't send arrays), best way send response in json format. can parse json string array.

http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/

json easy use (usually) way send strings , dump them arrays , vice versa.

there's ways in java (which assume language server-side code) , php (which many people use also) , every other language.

for reason recall java ones being more difficult use should be, i'm mistaken. either way, it's not hard learn , bring code-fu next level sure.


a hack-y, not correct, ugly way using raw string is:

// remove first , last bracket var data = resultdata.substr(1, resultdata.length - 2); // split array data = data.split(", "); 

...but please don't way, educational purposes (at risk of downvote). haven't tested idea.

again, best way change design use json everywhere.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -