最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - url encoding in xmlhttprequest - Stack Overflow

programmeradmin1浏览0评论

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 .html
it didn't work for me

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

Share Improve this question edited Mar 21, 2012 at 10:52 Sai Kalyan Kumar Akshinthala 11.8k8 gold badges45 silver badges68 bronze badges asked Mar 21, 2012 at 10:51 John silverJohn silver 1051 gold badge6 silver badges14 bronze badges 3
  • If you want to encode a string use encodeURIComponent('your'+ string) or escape('your'+ string). Hope it helps you. – Jan Wiemers Commented Mar 21, 2012 at 10:54
  • it didnt help in my case...the problem that in the url localhost/day1/tryconnect.php?add="+address (+address) is a special character word..and its being sent to php code via ajax request using the xmlhttprequest object...the special all am looking for is to probably encode this address in utf-8 so that it won't make any problems dealing with Chrome and IE..apparently the address is received corrupted and not encoded properly... – John silver Commented Mar 21, 2012 at 11:07
  • solved :) stackoverflow./questions/332872/… – John silver Commented Mar 21, 2012 at 11:29
Add a ment  | 

1 Answer 1

Reset to default 2

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

It uses a bination of encodeURI + escape

encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
发布评论

评论列表(0)

  1. 暂无评论