Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  190] [ 4]  / answers: 1 / hits: 47427  / 14 Years ago, tue, may 11, 2010, 12:00:00

How can I send a JavaScript array as a JSON variable in my AJAX request?


More From » ajax

 Answers
6

This requires you to serialize the javascript array into a string, something that can easily be done using the JSON object.



var myArray = [1, 2, 3];
var myJson = JSON.stringify(myArray); // [1,2,3]
....
xhr.send({
data:{
param: myJson
}
});


As the JSON object is not present in older browsers you should include Douglas Crockfords json2 library



If you already rely on some library that includes methods for encoding/serializing then you can use this instead. E.g. ExtJs has Ext.encode


[#96811] Saturday, May 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryonk

Total Points: 161
Total Questions: 116
Total Answers: 107

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
bryonk questions
;