Reputation: 19
I am making an ajax call and getting back result from array that looks like result = [one, two, three];
I need to convert this to an array and iterate it. Since my string has no quotes around values array isn't read correctly.
Here is my code, if you can please show me correct way to do 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 to make valid array to iterate it
for (var i = 0; i < resultData.length; i++) {
console.log(resultData[i]);
}
}
};
Upvotes: 1
Views: 3974
Reputation: 9576
Though I don't know exactly why you can't output a valid JSON (with quoted strings in your array), this little and dirty function can parse your "almost-array" to a valid one, by putting quotes and removing spaces, this function returns a real array.
function validArray(str){
return str
.replace(/[\[\]']+/g,"")
.split(",")
.map(function(v){
return v.replace(/^\s\s*/g, '').replace(/\s\s*$/g, '');
});
}
The best solution is to avoid this kind of parsing and respond with valid JSON. This way, you could simply do a JSON.parse and easily obtain a JS object.
Upvotes: 0
Reputation: 57209
Hi "user" and welcome to SO. Thanks for the code sample and well-formatted question :)
Since HTTP requests use strings (they don't send arrays), the best way to send your response is in JSON format. You can then easily parse the JSON string into an array.
http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON is a really easy to use (usually) way to send strings and dump them into arrays and vice versa.
There's ways to do it in Java (which I assume is the language for your server-side code) and PHP (which many people use also) and every other language.
For some reason I recall the Java ones being more difficult to use than they should be, but hopefully I'm mistaken. Either way, it's not too hard to learn and will bring your code-fu to the next level for sure.
A hack-y, not correct, ugly way to do it using your raw string is:
// Remove first and last bracket
var data = resultData.substr(1, resultData.length - 2);
// Split the array
data = data.split(", ");
...but please don't do it this way, this is for educational purposes only (at the risk of a downvote). Also I haven't tested it but you get the idea.
Again, the best way to do it is change your design to use JSON everywhere.
Upvotes: 1
Reputation: 4592
Try this, its a little more complex, but it'll work:
resultData = xmlhttp.responseText;
resultData = resultData.substring(1, resultData.length-1);
var resultArray = resultData.split(',');
Upvotes: 0
Reputation: 88378
Ideally, you would use the JSON.parse function, but since one
, two
, and three
are not quoted, you will have to do some work.
You can run through each of the items in your square brackets and put double quotes around them, then call JSON.parse
. Alternatively, do as Steve suggests and rewrite your server's handler to respond with proper JSON.
If they were quoted, JSON.parse would work like this:
> JSON.parse('["one", "two", "three"]')
["one", "two", "three"]
Upvotes: 0