Reputation: 39
I need to pass an array with AJAX in a ASP page.
I tried passing it as GET to see the data that it sends and i noticed that the ASP receive only the LAST record of the array.
How can i send and receive the whole array? (preferably with POST method)
Javascript
function editar(i) {
arr=[];
j=0;
for (n=2; n<=9; n++) {
arr[j]=$("#"+i+"-"+n).val(); //saves in the array all the field of a table's row
}
$.ajax ({
url:"page.asp",
data: { arr:arr },
type:"POST"
}).done(function(msg){
alert(msg)
})
}
ASP page.asp
<%
redim arr(10)
for i=0 to 9
arr(i)=request.Form("arr[]") 'Tried without the square brackets too
next
%>
Upvotes: 0
Views: 2766
Reputation: 3854
An html form doesn't have an array data structure. In fact, an html form doesn't really have data types: pretty much everything you put into a form will come back out as a string. So your best bet is probably to write the array into a string (via .join()
), which you can then parse on the other end (using, e.g. Split()
).
Keeping in mind that I'm terrible at JavaScript, I think you'd do something like
data: { 'arr':arr.join('+++') }
and then in your VBScript, you'd do
dim arr '- note that you're NOT dimming as an array
arr = Split(Request.Form("arr"),"+++")
Note that in practice, whenever I use Split, I add a delimiter to the end, just to make sure that Split won't throw an error.
const delimiter = "+++"
arr = Split(Request.Form("arr") & delimiter,delimiter)
Upvotes: 1
Reputation: 70
function editar(i) {
arr=[];
j=0;
for (n=2; n<=9; n++) {
arr[j]=$("#"+i+"-"+n).val(); //saves in the array all the field of a table's row
}
$.ajax ({
url:"page.asp",
data: { 'arr[]':arr },
type:"POST"
}).done(function(msg){
alert(msg)
})
}
i had to change your "arr" in the data object to 'arr[]' and that will do
use the snippet below on the server side C#
string[] values = Request.Form.GetValues("arr[]");
Vb
Dim values As String() = Request.Form.GetValues("arr[]");
Upvotes: 0