views:

179

answers:

5

Hi, I'm doing this:

$.ajax({  
    type: "POST", url: baseURL+"sys/formTipi_azioni",data:"az_tipo="+azione,
    beforeSend: function(){$("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');},
    success: function(html){$("#form").html(html);}  
 });

there is a case when azione is

TB+ 

the plus sign doesn't get POSTed at all, a blank space get sent. I already tried this:

azione = escape(String(azione));

With no luck. Does anybody knows how to fix this?

Thank you

+7  A: 

Never use escape(). Use encodeURIComponent().

Tomalak
+3  A: 
azione = escape(String(azione));

should be

azione = encodeURIComponent(String(azione));

or simply

azione = encodeURIComponent(azione);
oezi
Type-casting doesn't work like that in JS... (`(String)azione`)
J-P
sorry, just a typo, i corrected that. thanks for the hint.
oezi
+2  A: 

you need encodeURIComponent

just somebody
+3  A: 

Instead of trying to compose the post data yourself, you can also let jQuery do the work by passing it an object:

$.ajax({  
    type: "POST", url: baseURL+"sys/formTipi_azioni",
    data: {az_tipo: azione},
    beforeSend: function(){$("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');},
    success: function(html){$("#form").html(html);}  
 });
VoteyDisciple
+8  A: 

Try this:

$.ajax({  
    type: "POST", 
    url: baseURL + "sys/formTipi_azioni",
    data: { az_tipo: azione },
    beforeSend: function(){
        $("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');
    },
    success: function(html){
        $("#form").html(html);
    }  
});

and leave jQuery do the url encoding for you.

Darin Dimitrov
Nice, I never knew this was possible. Thank you.
0plus1
It is possible and the benefit is that you don't have to worry about url encoding.
Darin Dimitrov
+1, best solution in this context. "Never use `escape()`" is still a point to keep in mind.
Tomalak