Results 1 to 8 of 8

Thread: Posting data using XMLHttpRequest

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Posting data using XMLHttpRequest

    Hi,

    I need to post the data using XMLHttpRequest. While posting, few values have to be passed in the URL like http://site.com?var1=value1&var2=value2 and few values have to be sent as form data.

    Please suggest how to construct the required headers and message format to post the data in this format using XMLHttpRequest in VB Script.

    If I try to set the URL as http://site.com?var1=value1&var2=value2 for the open method of XMLHttpRequest and the other values related form data by specifying content-disposition and the name of the variable and trying to send the data, looks like the variable values passed in the URL doesn't seem to be recognized by the target server.

    Thanks in advance,

    Raghu

  2. #2
    Addicted Member
    Join Date
    Jul 2009
    Posts
    208

    Re: Posting data using XMLHttpRequest

    It's difficult to help you if you don't say what site it is AND post your code.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Posting data using XMLHttpRequest

    Probably don't need to know what site it is, but it sounds like you've tried something already and it didn't work - it'd be helpful to post the code you tried.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Posting data using XMLHttpRequest

    Here is the portion of the code I have used to post the data using XMLHttpRequest object

    Code:
    	Const MULTIPART_BOUNDARY = "9876543210----------0123456789"
    
    	Dim strResponse, xmlhttp, xmlDom, returnVal
    	Dim strData
    
    	strData = ""
    
    	strData = strData & "--" & MULTIPART_BOUNDARY & vbCrLf
    	strData = strData & "Content-Disposition: form-data; name=""message""" & vbCrLf & vbCrLf
    	strData = strData & strMessage
    	strData = strData & vbCrLf
    
    	strData = strData & "--" & MULTIPART_BOUNDARY & "--"
    
    	Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    	xmlhttp.open "POST", "http://mysite.com?environmentId=Development", false
    
    	xmlhttp.SetRequestHeader "Accept", "application/x-www-form-urlencoded,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
    	xmlhttp.SetRequestHeader "Accept-Language", "en-us,en;q=0.5"
    	xmlhttp.SetRequestHeader "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    	xmlhttp.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
    	xmlhttp.SetRequestHeader "Content-Length", Len(strData) + 2
    	xmlhttp.Send strData
    
    	'If the state of the request is 4 then get the response and parse it
    	If xmlhttp.readyState = 4 Then
    		'Get the response and set it to a string
    		strResponse = xmlhttp.responseText	
    
    		'Create DOM object reference and parse the xml string
    		Set xmlDom = CreateObject("Msxml2.DOMDocument")
    		xmlDom.loadxml(strResponse)
    		'Echo if any error while parsing
    		If xmlDom.parseerror.errorcode <> 0 Then
    			Wscript.echo "Reason for the error:" & xmlDom.parseerror.reason
    		Else
    			retVal = xmlDom.text
    			Wscript.echo "Reason for the error:" & retVal
    		End If
    	End If
    In the above code, I passing environmentID value as part of URL and message value as a form variable as the server expects the data in this format. But based on the response from the server it appears that Environment ID value is not recognized. As the server returns that as the error, I am not sure whether the message variable passed in the form data is recognized or not. The message parameter value is an XML which will be read from an XML file (Not shown in the code). I am not sure whether is it required to set the message type or not.

    Please suggest what is wrong in the above code for my requirement.

  5. #5
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Posting data using XMLHttpRequest

    I don't see anything wrong here, and testing it out for myself worked correctly (environmentID was recognized). Do you have access to the code on the server? Do you know how it's trying to access that variable?

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Posting data using XMLHttpRequest

    Hi,

    I do not have access to the code on the server. However, I tried posting it again. Looks like environmentID is recognized. However, message value that is passed like form data is not recognized. Is something wrong in constructing the form data to be sent to the server?

    Thanks in advance.

    Raghu

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Posting data using XMLHttpRequest

    Your strData should be set up in the following format:
    Code:
    strData = "var1=value1&var2=value2"
    Etc. I don't believe you need anything about content disposition or any other data in the string except for variable and value pairs.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Posting data using XMLHttpRequest

    Hi,

    I have simulated my requirement by creating a web page that posts the data to the server and verified HTTP headers sent by the browser. The browser used the content-type as application/x-www-form-urlencoded and the XML file content also encoded to URL format, which I was not doing in my code. Following code worked fine for my requirement.

    Code:
    	Dim strResponse, xmlhttp, xmlDom, returnVal
    	Dim strMessage
    
    	Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    	xmlhttp.open "POST", "http://mysite.com?environmentId=Development", false
    
    	xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    	xmlhttp.SetRequestHeader "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*"
    	xmlhttp.SetRequestHeader "Accept-Language", "en-us"
    	xmlhttp.SetRequestHeader "Accept-Encoding", "gzip, deflate"
    
    	'Send the request synchronously by converting the encoding the message to be sent
    	xmlhttp.send "message=" & escape(strMessage)
    
    	'If the state of the request is 4 then get the response and parse it
    	If xmlhttp.readyState = 4 Then
    		'Get the response and set it to a string
    		strResponse = xmlhttp.responseText	
    
    		'Create DOM object reference and parse the xml string
    		Set xmlDom = CreateObject("Msxml2.DOMDocument")
    		xmlDom.loadxml(strResponse)
    		'Echo if any error while parsing
    		If xmlDom.parseerror.errorcode <> 0 Then
    			Wscript.echo "Reason for the error:" & xmlDom.parseerror.reason
    		Else
    			retVal = xmlDom.text
    			Wscript.echo "Reason for the error:" & retVal
    		End If
    	End If

    Thank you,
    Raghu

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width