1

I'm calling a webservice using jQuery with .ajax

Here are the data parameters for the call:

  var parameters = "{'Titre':'" + Titre + "','Description':'" + Description + "','Contact':'" + Contact + "','VilleId':'" + VilleId + "','QuartierId':'" + QuartierId + "','UserId':'" + UserId + "'}";

It works fine. But when parameters Description or Titre contain the ' character , no call!!!

Does anyone have an idea how can i make it work even with apostrophe character in Titre and/or Description?

Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
  • Possible duplicate of http://stackoverflow.com/questions/1470768/how-to-escape-apostrophe-or-quotes-on-a-jsp-used-by-javascript – Lance Jan 28 '11 at 17:29

5 Answers5

3

I would use a json encoder. Douglas Crockford's JSON in JavaScript seems a good choice.

Then you just write

 var param = JSON.stringify({ 'Titre': Titre, 'Description': Description });

and let the master worry about the quoting.

Tobi Oetiker
  • 5,167
  • 2
  • 17
  • 23
  • How can i precise that the question is answered. –  Jan 28 '11 at 17:53
  • When you have decided which answer is the most helpful to you, mark it as the accepted answer by clicking on the check box outline to the left of the answer. This lets other people know that you have received a good answer to your question. Doing this is helpful because it shows other people that you’re getting value from the community. (If you don’t do this, people will often politely ask you to go back and accept answers for more of your questions!) -- http://stackoverflow.com/faq – Tobi Oetiker Jan 29 '11 at 06:03
2

Try escaping the apostrophe:

    var parameters = "{
         'Titre':'" + Titre.replace(/'/g,"\'") + 
//                          ^
        "','Description':'" + Description + 
        "','Contact':'" + Contact + 
        "','VilleId':'" + VilleId + 
        "','QuartierId':'" + QuartierId + 
        "','UserId':'" + UserId + "'}";
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

You probably need to encode the values to be safely passed in a URL.

http://plugins.jquery.com/project/URLEncode

Arseny
  • 5,159
  • 4
  • 21
  • 24
0

You can try escaping it:

var str = "asdfsd'asdfadf";
str = str.replace("'", "\'");
capdragon
  • 14,565
  • 24
  • 107
  • 153
0

Here's the way I escape that works for me currently:

var theString = "O'Kief blahblahblahblah";
theString = theString .replace("'", "\\'");
//Note the double \\ 

Doesn't break and saves as: O'Kief blahblahblahblah

t..
  • 1,101
  • 1
  • 9
  • 22