...

View Full Version : write parser for sending/receiving data



carlitos_way
10-10-2008, 12:01 PM
Hi All,

I have an issue in my web/dynamic program about the parsing of characters when sending them to the server.

I use AJAX to make a post request of the variables of the form.
As normal, when sending characters as '&', and all the other special characters I receive error. At start I tried to use the json parser from json.org, but it gives me conflicts with the code in my application so I decided to write a parser on my own... therefore I'm writing here to ask which is the best way to do it:

how should I replace the special characters like '&', '%', etc?
with exadecimal codes?

do you have any suggestion to avoid making big errors in writing it?

Thanks
CB

A1ien51
10-10-2008, 04:10 PM
Have you tried to use encodeURIComponent (http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent)

Eric

Basscyst
10-10-2008, 07:09 PM
You might find this handy too:



//*******************************************************
// Builds a querystring of the child form elements of the
// object passed to the function.
//*******************************************************
function formPost(top_element){
var inputs=top_element.getElementsByTagName('*');
var qstring=new Array();
for(var i=0;i<inputs.length;i++){
if(!inputs[i].disabled&&inputs[i].getAttribute('name')!=""&&inputs[i].getAttribute('name')){
qs_str=inputs[i].getAttribute('name')+"="+encodeURIComponent(inputs[i].value);
switch(inputs[i].tagName.toLowerCase()){
case "select":
if(inputs[i].getAttribute("multiple")){
var len2=inputs[i].length;
for(var j=0;j<len2;j++){
if(inputs[i].options[j].selected){
var targ=(inputs[i].options[j].value) ? inputs[i].options[j].value : inputs[i].options[j].text;
qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
}
}
}
else{
var targ=(inputs[i].options[inputs[i].selectedIndex].value) ? inputs[i].options[inputs[i].selectedIndex].value : inputs[i].options[inputs[i].selectedIndex].text
qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
}
break;
default:
switch(inputs[i].getAttribute("type").toLowerCase()){
case "radio":
if(inputs[i].checked){
qstring[qstring.length]=qs_str;
}
break;
case "checkbox":
if(inputs[i].value!=""){
if(inputs[i].checked){
qstring[qstring.length]=qs_str;
}
}
else{
if(inputs[i].checked){
qstring[qstring.length]=inputs[i].name+"=ON";
}
}
break;
default:
qstring[qstring.length]=qs_str;
break;
}
break;
}
}
}
return qstring.join("&");
}

carlitos_way
10-13-2008, 09:23 AM
Have you tried to use encodeURIComponent (http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent)

Eric

Yes, it worked by using, in the requests


encodeURIComponent(escape(str))

while in the responses


unescape(str)

Thanks!
CB

rnd me
10-13-2008, 02:14 PM
encodeURIComponent(escape(str))


you shouldn't need to double encode it like that. you increase the byte overhead required and you could run into conflicts later.

hey; working is working, but it's worth the time to nail down exactly what's going on with each of the two functions...

$0.02

carlitos_way
10-14-2008, 09:32 AM
you shouldn't need to double encode it like that. you increase the byte overhead required and you could run into conflicts later.

hey; working is working, but it's worth the time to nail down exactly what's going on with each of the two functions...

$0.02

Hi rnd_me, I noticed the byte overhead but I did a few tests before deciding what to use, and I noticed that when I was using only the encodeURIComponent function, I had a problem during the response (malformed URI sequence). Then I found by googling (http://trac.openlayers.org/ticket/1704) that someone was having the same problem, andd using his solution it started to work for me.

If you have suggestions on how to fix this, it's well accepted.. for me it was a big headache from the start... :mad:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum