Search code examples
javascriptasp.netjqueryajaxwebservice-client

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?


Solution

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