John silver
John silver

Reputation: 105

url encoding in xmlhttprequest

Iam trying to encode url of the xmlhttprequest method as utf-8

var Url=("http://localhost/day1/tryconnect.php?add="+address)  ;       // the server script to handle the request
if (window.XMLHttpRequest) {
       xmlhttp= new XMLHttpRequest() ;        // For all modern browsers
       } 

 else if (window.ActiveXObject) {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") ;   // For (older) IE
 }
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("connection is stable") ;
}


 xmlhttp.open("GET", Url, false);   
 xmlhttp.send(null);
 var xml = xmlhttp.responseXML ; 

i tried the following method to encode the Url http://www.webtoolkit.info/javascript-utf8.html
it didn't work for me

Upvotes: 0

Views: 12800

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23113

Here's a great article on the subject of URLEncoding/Decoding in JavaScript: http://roneiv.wordpress.com/2007/12/25/how-to-do-proper-url-encoding-in-javascript-when-using-windowopen/

It uses a combination of encodeURI + escape

encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));

Upvotes: 2

Related Questions