开发者

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 w开发者_高级运维hen 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?


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.


Try escaping the apostrophe:

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


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

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


You can try escaping it:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜