Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb write parser for sending/receiving data

    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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Have you tried to use encodeURIComponent

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    carlitos_way (10-13-2008)

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    You might find this handy too:

    Code:
    //*******************************************************
    // 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("&");
    }
    Last edited by Basscyst; 10-10-2008 at 07:16 PM.
    Helping to build a bigger box. - Adam Matthews

  • #4
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by A1ien51 View Post
    Have you tried to use encodeURIComponent

    Eric
    Yes, it worked by using, in the requests

    Code:
    encodeURIComponent(escape(str))
    while in the responses

    Code:
    unescape(str)
    Thanks!
    CB

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,375
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by carlitos_way View Post
    Code:
    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
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #6
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rnd me View Post
    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 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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •