简体   繁体   中英

Send an Array with Ajax and receive it in ASP Classic page

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
%>

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, eg 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)

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[]");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM