user594166
user594166

Reputation:

problem with apostrophe in ajax webservice call

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?

Upvotes: 1

Views: 8009

Answers (5)

t..
t..

Reputation: 1101

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

Upvotes: 0

capdragon
capdragon

Reputation: 14899

You can try escaping it:

var str = "asdfsd'asdfadf";
str = str.replace("'", "\'");

Upvotes: 0

Tobi Oetiker
Tobi Oetiker

Reputation: 5460

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.

Upvotes: 3

KooiInc
KooiInc

Reputation: 122936

Try escaping the apostrophe:

    var parameters = "{
         'Titre':'" + Titre.replace(/'/g,"\'") + 
//                          ^
        "','Description':'" + Description + 
        "','Contact':'" + Contact + 
        "','VilleId':'" + VilleId + 
        "','QuartierId':'" + QuartierId + 
        "','UserId':'" + UserId + "'}";

Upvotes: 2

Arseny
Arseny

Reputation: 5179

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

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

Upvotes: 0

Related Questions