javascript - problem with apostrophe in ajax webservice call - Stack Overflow

I'm calling a webservice using jQuery with .ajaxHere are the data parameters for the call:var para

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?

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?

Share Improve this question edited Jan 28, 2011 at 17:30 Geoff Appleford 18.8k4 gold badges65 silver badges87 bronze badges asked Jan 28, 2011 at 17:23 user594166user594166 1
  • Possible duplicate of stackoverflow./questions/1470768/… – Lance Commented Jan 28, 2011 at 17:29
Add a ment  | 

5 Answers 5

Reset to default 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.

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./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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744213354a4563439.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信