2

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"

test@test.com
-----------------------------254001430938683980861095915686
Content-Disposition: form-data; name="contact_message"

my message
-----------------------------254001430938683980861095915686--
Dennis
  • 708
  • 2
  • 10
  • 25
  • 2
    The problem is you are not posting the form as `application/x-www-form-urlencoded` that output is using `multipart/form-data` which can only be read as a raw binary stream by Classic ASP. Switch to urlencoded and it will work as you are expecting. – user692942 Dec 05 '20 at 15:42
  • 1
    Does this answer your question? [AJAX - Classic ASP - Post a Form](https://stackoverflow.com/questions/36489995/ajax-classic-asp-post-a-form) – user692942 Dec 05 '20 at 15:43
  • 2
    that's it!!! thank you for the tip & lead!!! it's coming back as binary data, so I need to use BinaryRead to read it. I just created a test that emails me the data and I got it all! Now, I have to figure out how to PARSE this response. This is helpful, i cannot cannot seem to get the values out at the end. Hard to test too since you cannot use response.write on BinaryRead. https://stackoverflow.com/questions/36489995/ajax-classic-asp-post-a-form/36732380#36732380 – Dennis Dec 05 '20 at 16:25
  • If you can switch the form `enctype` to `application/x-www-form-urlencoded` then the data can be access using `Request.Form` as normal. You will enter a world of pain trying to manually decode `multipart/form-data` using `Response.BinaryRead()`. – user692942 Dec 05 '20 at 16:42
  • oh, i know!! I wish I could change it, I cannot. Trying to write a script to access the fields by parsing. What a PITA!!! The one at the article above will not work, i played around with it but cannot seem to figure out the issue. – Dennis Dec 05 '20 at 17:00
  • ASPupload by persits was the BETTER answer, added below. – Dennis Dec 05 '20 at 18:22
  • So you are trying to upload a file? That would make sense then, but your sample or question didn’t mention that. – user692942 Dec 05 '20 at 20:40
  • 1
    Does this answer your question? [How to upload files with asp-classic](https://stackoverflow.com/a/12200970/692942) – user692942 Dec 05 '20 at 20:43
  • 1
    Lankymart, I was not uploading files - just handling a POST request with unchangeable content type. ASPupload was one solution I used to get the data - much easier (i now know!) than writing my own below. – Dennis Dec 06 '20 at 05:14
  • Don’t understand why you can't modify the AJAX request to send the form post using `application/x-www-form-urlencoded`? That way you could just access the values with `Request.Form` without having to use 3rd party COM components or parsing the raw binary manually? The question doesn’t make it clear. – user692942 Dec 06 '20 at 10:22
  • I agree that is best, I do not have access to the script though to change it. – Dennis Dec 06 '20 at 14:46
  • Does this answer your question? [ASP: request.form is not returning value?](https://stackoverflow.com/a/3661121/692942). – user692942 Dec 06 '20 at 15:52

1 Answers1

2

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
Dennis
  • 708
  • 2
  • 10
  • 25