Utku Dalmaz
Utku Dalmaz

Reputation: 10192

Special character problem in ajax and javascript

there is a textarea on the page. and i am sending its value via ajax.

var text = $("textarea#text").val();

var dataString = 'text='+ text;


        $.ajax({
      type: "POST",
      url: "do.php?act=save",
      data: dataString,
      cahce: false,
      success: function() {

                   //success


            }

     });

if textarea value is sth like that black & white , it breaks text after the black

if it is sth like that black + white it outputs like black white

how can i avoid this?

thx

Upvotes: 0

Views: 1971

Answers (4)

Anurag U R
Anurag U R

Reputation: 1

you can achieve this by using JSON object

eg: [{"AttributeId":"4035","Value":"Street & House"}]

or you can use URLencode before post

Upvotes: 0

o.k.w
o.k.w

Reputation: 25820

Or JSON.stringify which converts a JSON object to string representation.

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827922

You need to encode the text, but I think is better to use an Object rather than a String as the data member, jQuery will do the job of properly encoding the POST/GET parameters:

var text = $("textarea#text").val();
var dataObj = {"text": text};
 $.ajax({
   type: "POST",
   url: "do.php?act=save",
   data: dataObj,
   cache: false,
   success: function() {
     //success
   }
 });

Upvotes: 1

Gabriel McAdams
Gabriel McAdams

Reputation: 58293

encodeURIComponent

Upvotes: 4

Related Questions