0plus1
0plus1

Reputation: 4555

Jquery ajax call with '+' sign

$.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?

Upvotes: 12

Views: 8458

Answers (6)

just somebody
just somebody

Reputation: 19257

you are looking for encodeURIComponent

Upvotes: 3

mDEV123
mDEV123

Reputation: 31

escape(String(azione)).replace(new RegExp( "\\+", "g" ),"%2B");

this one sends the plus symbol with the help of regular expression

Upvotes: 2

oezi
oezi

Reputation: 51817

azione = escape(String(azione));

should be

azione = encodeURIComponent(String(azione));

or simply

azione = encodeURIComponent(azione);

Upvotes: 16

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

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.

Upvotes: 14

VoteyDisciple
VoteyDisciple

Reputation: 37813

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);}  
 });

Upvotes: 6

Tomalak
Tomalak

Reputation: 338326

Never use escape(). Use encodeURIComponent().

Upvotes: 9

Related Questions