Error when passing quotes to webservice by AJAX

Posted by Radu on Stack Overflow See other posts from Stack Overflow or by Radu
Published on 2010-12-27T23:20:40Z Indexed on 2010/12/28 0:53 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

I'm passing data using .ajax and here are my data and contentType attributes:

data: '{ "UserInput" : "' + $('#txtInput').val() + '","Options" : { "Foo1":' + bar1 + ', "Foo2":' + Bar2 + ', "Flags":"' + flags + '", "Immunity":' + immunity + '}}',
contentType: 'application/json; charset=utf-8',

Server side my code looks like this:

<WebMethod()> _
Public Shared Function ParseData(ByVal UserInput As String, ByVal Options As Options) As String

The userinput is obvious but the Options structure is like the following:

Public Structure Options
    Dim Foo1 As Boolean
    Dim Foo2 As Boolean
    Dim Flags As String
    Dim Immunity As Integer
End Structure

Everything works fine when $('#txtInput') contains no double-quotes but if they are present I get an error (for an input of asd"):

{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (22): { \"UserInput\" : \"asd\"\",\"Options\" : { \"Foo1\":false, \"Foo2\":false, \"Flags\":\"\", \"Immunity\":0}}","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

Any idea how I can avoid this error? Also, when I pass the same input with quotes directly it works fine.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about jQuery