Note that there are some explanatory texts on larger screens.

plurals
  1. POSubmission with single quotes using jQuery, Ajax and JSON
    text
    copied!<p>I've looked around, and I'm trying to find an elegant solution to this, and I'm yet to find one. I have an ASMX web service in .NET that I'm trying to call that requires parameters.</p> <p>I'm using jQuery on the client side to call the service and my jQuery code looks something like this:</p> <pre><code>$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", url: "/Reviews/HotelReview.asmx/SubmitReview", data: "{'name': '" + name + "', " + "'info': '" info + "'}", processData: true, beforeSend: function() { startSubmit(); }, complete: function() { submitComplete(); }, error: function(xhr) { submitError(xhr); }, success: function(msg) { submitSuccess(msg.d); } }); </code></pre> <p>It works very well, except when either <strong>name</strong> or <strong>info</strong> contain the <strong>'</strong> character, a single quote. Simple enough, because my JSON defines the end of the value of the field and is a single quote. When either of these fields contains a single quote, all I get is an "Internal Server Error", but further inspection using <a href="http://en.wikipedia.org/wiki/Fiddler_%28software%29" rel="nofollow noreferrer">Fiddler </a> showed me the results (I won't bother to post them) indicating the single quote issue.</p> <p>I've put something in place temporarily to remove the single quotes on the client side and put them back in on the server side, but this is far from elegant. Is there a more elegant way to escape these single quotes so that my code can work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload