Error when passing quotes to webservice by AJAX
- by Radu
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.