Fernando Fernando - 6 months ago 95x
Javascript Question

AJAX - Classic ASP - Post a Form

I have a TEST.ASP with this code:

<SCRIPT src="ajaxScript.js" type="text/javascript"></SCRIPT>
<FORM action="action_page.asp" method="post">
First Name:<BR>
<INPUT type="text" name="FName"><BR>
Last Name:<BR>
<INPUT type="text" name="LName"><BR>
<INPUT type="submit" value="Submit">
<BUTTON type="button" onClick="loadXMLDoc('action_page.asp',this.form);">GoGoGo!</BUTTON>
<DIV id="msgBoxDiv">TEST!!</DIV>

The Javascript file that is called (ajaxScript.js) has this code:

var req; // global variable to hold request object

function processReqChange()
if (req.readyState == 4 && req.status == 200){document.getElementById("msgBoxDiv").innerHTML = req.responseText;}

function loadXMLDoc(url, params)
req = new XMLHttpRequest();
} catch(e)
req = false;
req = false;

var formData = new FormData(params);

req.onreadystatechange = processReqChange;
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
return true;
return false;

And my "action_page.asp" to receive the parameters is like so:

vRF1 = request.Form("FName")
vRF2 = request.Form("LName")



If I make the normal submit (submit button), everything goes as expected: it shows a new page with the values of the form.

BUT... if I try to read the target ASP with AJAX (gogogo button), I can't send the form to the target page. I do receive the target page, but without the supposed values.
I get this:

Result page

If I change "req.send(formData);" for " req.send("FName="+1+"&LName=QWER");", everything works well.

I've read that to send the entire form (like the "usual" post do), I just need to make "var formData = new FormData(params);" where params would be the form object, and then send the FormData(params).

What may I be doing wrong here?


I didn't want to leave this without an answer.

Lankymart gave the path to the right answer... the truth is, when I submitted the "new FormData(formID)" I was sending in multipart/form-data and not application/x-www-form-urlencoded as I was declaring.

SO... We can make the hypothesis Lankymart indicated (application/x-www-form-urlencoded). In other words, you just take my initial question, and in req.send(); you have to form the string of the parameters to pass (just like in a GET request). "name1="+param1+"&name2="+param2. I opted by this one, because, for what I needed, this was more than enough.

OR... If we do need/wish to send the form - req.send(formData)... that will produce a multipart/form-data (check an example of the format of a multipart/form-data here: http://www.codeproject.com/Articles/1125/Advanced-ASP-Uploader) In this case... you have to build a parser for the info. Here is what I've coded (just to try a thing or two):

Function StoreNameAndValues(tempVarArray)
    Dim tempVar

    for i = 1 to ubound(tempVarArray)-1
        if tempVarArray(i)<>"" then
            tempVar=Split(tempVarArray(i), """")

            if ubound(tempVar)>1 then
            End if
        End if
End Function



separator=Split(tempVar1, CHR(13)&CHR(10))(0)

Dim postNamesArray()
Dim postValuesArray()
ReDim Preserve postNamesArray(postArgumentsSize)
ReDim Preserve postValuesArray(postArgumentsSize)


In this example, I've build a very rudimentary parser... it's not prepared for things like file upload, but this is just an example of what to do.

Hope I didn't make any big mistake... and it will help someone.