This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes HTML, CSS and JavaScript and the fly likes problem with sending # character through ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem with sending # character through ajax" Watch "problem with sending # character through ajax" New topic
Author

problem with sending # character through ajax

darshan karle
Greenhorn

Joined: May 09, 2008
Posts: 28
hello every body

i am trying to send #20 number using AJAX and displaying names based on the number.But my problem is it not received in the server side. if i send as 20 then it is recieved. please help out in this. i am placing the code below



<script type="text/javascript">
var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="get1.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>



<body>
<form>
Number:
<input type="text" id="txt1" onkeyup="showHint(this.value)">
</form>

Suggestions: <span id="txtHint"></span>


</body>


regards
darshan
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

darshan karle wrote:hello every body

i am trying to send #20 number using AJAX


The '#' symbol ('pound' symbol) have special meaning in URL, its is used to indicate where a fragment identifier (bookmarks/anchors in HTML) begins in URL.

So avoid it, but if you still want to use it, then encoded it with '%' symbol concatenated by its hex(23) value, then URL looks like,,

http://mysite.com/app/servlet?param=%23

'%23' is for '#'.

More information can be found here.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
darshan karle
Greenhorn

Joined: May 09, 2008
Posts: 28
thanks rohan for the help

regards
darshan
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

darshan karle wrote:thanks rohan for the help


You're welcome . And my name is Sagar, 'rohan' is part of my surname.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You should be using encodeURIComponent: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent

Eric
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Eric Pascarello wrote:You should be using encodeURIComponent: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent

Eric

Winner
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem with sending # character through ajax