...

View Full Version : How to Escape "&" on xmlHttp



MirceaP
06-18-2009, 09:42 AM
Hello,
I have an Ajax submit xmlHttp. It works fine except some special characters like the ampersand do not pass and cut the submitted text. I understood that I could use the "escape" function to solve this. I have no idea how to use escape on my Java code.

Here is my code, how can I add the "escape function"?



<script type="text/javascript">
var fieldname = 'postrowbody';
var xmlHttp = http_object();

function get(f)
{
var param = elem = '';
for(var s = 0; s < f.elements.length; s++)
{
elem = f.elements[s];
if(param != '')
{
param += '&';
}
param += elem.name + '=' + elem.value;
}
document.getElementById(fieldname).innerHTML += "<div id='load' class='preloader'><img src='/tabs/loading.gif' /><p>Requesting content...</p></div></div>";

xmlHttp = http_object();
xmlHttp.onreadystatechange = handle_return;
xmlHttp.open("POST", '{S_AJAX_ACTION}', true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(param);

//clear the message site
document.postform.message.value = '';
}

function handle_return()
{
if (xmlHttp.readyState == 4)
{
var load = document.getElementById('load');
load.parentNode.removeChild(load);
document.getElementById(fieldname).innerHTML += xmlHttp.responseText;
document.postform.reset();
}
}

function http_object()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e)
{
try
{
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
return xmlHttp;
}
</script>


Thank you

oesxyl
06-18-2009, 10:48 AM
try this way:


<script type="text/javascript">
var fieldname = 'postrowbody';
var xmlHttp = http_object();

function get(f)
{
var param = elem = '';
for(var s = 0; s < f.elements.length; s++)
{
elem = f.elements[s];
if(param != '')
{
param += '&amp;';
}
param += elem.name + '=' + escape(elem.value);


best regards

Philip M
06-18-2009, 10:58 AM
Example:-

var age = 37;
var name = 'John Doe';
var job = 'Dog Catcher';
var passData = 'age='+escape(age)+'&name='+escape(name)+'&job='+escape(job);


The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

MirceaP
06-18-2009, 11:30 AM
Thanx oesxyl & Philip M for the fast response. It did the trick!

oesxyl, can you also please tell me how to do if I want to another character besides "&" - things like ()#%&

Thanx again for your help!

Philip M
06-18-2009, 11:40 AM
See above Post#3.

escape('~!@#$%^&*(){}[]=:/,;?+\'"\\');
returns
%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C

oesxyl
06-18-2009, 11:46 AM
Thanx oesxyl & Philip M for the fast response. It did the trick!

oesxyl, can you also please tell me how to do if I want to another character besides "&" - things like ()#%&

Thanx again for your help!
you can use instead of chars their entities values:

http://www.evotech.net/blog/2007/04/named-html-entities-in-numeric-order/

but in xml only "&", "'", '"', '<', '>' are not permited so you can use '&amp;', '&apos;', '&quot;', '&lt;' and '&gt;' for them and utf-8 for the rest.

javascript escape will convert to %nn any char is not allwed and, as Philip M explain, assuming that paramaters names are valid you need to escape only their values.

best regards

MirceaP
06-18-2009, 11:49 AM
Thanx for your help, I really appreciate it!
All the best,
Mircea



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum