Dennis
Dennis

Reputation: 728

Classic ASP - Cannot Get Request.Form Values from AJAX Post Request

I have a script that is submitting a POST request of a form via AJAX.

When I look at the network tab, it is coming back in the format below, which I cannot read with a standard Request.Form in Classic ASP. I am seeing this server variable added to the page request as well due to the AJAX request: HTTP_X_REQUESTED_WITH = XMLHttpRequest

The form is set up as a simple POST: <form method="post" action="/contact-submit">

I cannot change the script performing the AJAX request to update the content type, etc.

This is the “Request payload” in the response on the network tab below. I have googled for days and cannot figure this out. How do you access this data? I even tried reading it with a file upload script I have via ASPUpload, but Upload.Form("contact_name") does not work either, it's blank as well.

I tried a simple PHP script (I do not know how PHP works, but this script came with script performing the POST as a demo), and calling print_r($_POST) the script passes all the correct info in an array back in the response on network tab. What the heck!!

Does anyone know how to get this data back in Classic ASP?

Thanks so much for the help in advance.

Dennis

-----------------------------254001430938683980861095915686
Content-Disposition: form-data; name="contact_name"

Test Name
-----------------------------254001430938683980861095915686
Content-Disposition: form-data; name="contact_email"

[email protected]
-----------------------------254001430938683980861095915686
Content-Disposition: form-data; name="contact_message"

my message
-----------------------------254001430938683980861095915686--

Upvotes: 2

Views: 1160

Answers (1)

Dennis
Dennis

Reputation: 728

I worked on a solution to reading the data, this works below. Not sure it is the best / least expensive way to do this, but it works!

Thanks to everyone for the help / tips. If anyone has a better way to parse the response above, I'm all ears :)

<%
Function BytesToStr(bytes)
   Dim Stream
   Set Stream = Server.CreateObject("Adodb.Stream")
      Stream.Type = 1 'adTypeBinary
      Stream.Open
      Stream.Write bytes
      Stream.Position = 0
      Stream.Type = 2 'adTypeText
      Stream.Charset = "iso-8859-1"
      BytesToStr = Stream.ReadText
      Stream.Close
   Set Stream = Nothing
End Function

If Request.TotalBytes > 0 Then
   Dim lngBytesCount, post
   lngBytesCount = Request.TotalBytes
   post = BytesToStr(Request.BinaryRead(lngBytesCount))
End If

Response.ContentType = "text/plain"

sContentType = Replace(Request.ServerVariables("CONTENT_TYPE"),"multipart/form-data; boundary=---------------------------","")

arrPost = Split(post,"-----------------------------" & sContentType)

For i = 0 to UBound(arrPost)
    sVal = Replace(arrPost(i),"Content-Disposition: form-data; name=","")
    arrVal = Split(sVal,Chr(10),1)

    For a = 0 to UBound(arrVal)

        If Instr(1, arrVal(a), "contact_name") <> 0 Then
            Response.Write GetValue(arrVal(a), "contact_name")
        End If
        If Instr(1, arrVal(a), "contact_message") <> 0 Then
            Response.Write GetValue(arrVal(a), "contact_message")
        End If

    Next
Next 

Function GetValue(f_FullString, f_FieldName)
    fieldval = Replace(f_FullString, """" & f_FieldName & """","")
    'response.Write "_" & fieldval & "_<Br>"
    arrVal1 = Split(fieldval,Chr(10),1)

    For b = 0 to UBound(arrVal1)
        newval = arrVal1(b)
        newval = Left(newval,Len(newval) - 2)
        newval = Right(newval,Len(newval) - 6)

        'For z = 1 to Len(newval)
        '   CurrChar = Mid(newval, z, 1)
        '   Response.Write Asc(CurrChar) & "<bR>"
        'Next
    Next
    GetValue = newval
End Function
%>

UPDATE:

This is another solution if you have ASPUpload installed. I tried this last night, but forgot to add Upload.Save (which would have saved me 4hours of work --- UGGGGGHHHH).

'http://www.aspupload.com/manual_memory.html
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save
Name = Upload.Form("contact_name")
Response.Write Name

Upvotes: 2

Related Questions