Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XMLHttp request url encoding failing

Status
Not open for further replies.

Navitas

Programmer
Jul 20, 2004
24
0
0
GB
Hi,

I am trying to use a XMLHttp POST to send data to an asp page on a website, using the following code :
Code:
  var xmlhttp;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		
		
	}
	xmlhttp.open("POST","[URL unfurl="true"]http://www.company.co.uk/quiz.asp",false);[/URL]
	xmlhttp.setRequestHeader("Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
	xmlhttp.send("name=Fred Flintstone");

Although the asp page receives and processes the data, the space is missing from the name value.

I tested the asp code by creating a standard HTML form and POSTing from that. This time the name was saved with white space preserved.

I have had the asp write back the entire contents of the POST and also the request.form("name") and got the following results:

XMLHttp:

response.write(Request.Form) > name=Fred Flintstone
response.write(Request.Form("name")) > FredFlintstone

Standard Http form:

response.write(Request.Form) > name=Fred+Flintstone
response.write(Request.Form("name")) > Fred Flintstone

From the above test it would appear that the url encoding is not working properly when sending the XMLHttp request.

Has anybody come across this before, or can spot something wrong with the code?

Many thanks

Darren
 
the code [tt][blue]xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");[/blue][/tt] simply sets a HTTP Header which tells the server the content type. It doesn't actually alter the data you subsequently send - you still need to encode this yourself.

By the way, your code is IE specific - there's an example of crossbrowser XMLHttpRequest code in thread216-1170297

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top