开发者

Error when passing quotes to webservice by AJAX

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

开发者_JAVA技巧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.


Have you tried escaping the text, or replacing a double-quote with a single quote, or replace the double-quote with the escape char: \"


Just to clarify the last answer ... The object is going to be evaluated server-side and as the field value is a string any double quotes within it need to be escaped - so should be \". Since this is also the js escape you need to escape both characters to \\"

$("#txtInput").val().replace(/\"\g, "\\\"") 


I meet the same problem.and this is my solution:

$.RFStringify = function(obj) {
  var result = obj;
  try {
    result = JSON.stringify(obj);
    result = result.replace(/([^\\])\\\"/g, '$1\\\\\\\"').replace(/([^\\])\"/g, '$1\\\"').replace(/(\")\"/g, '$1\\\"');
  } catch (ex) {
    // $.RFAlertEx(ex);
  } finally {
    return result;
  }
};
var a = {
  a: 'a',
  b: {
    b: 'B',
    c: '{"c":"c","d":{"d":"D","e":"E"}}'
  }
};
$('#result').html($.RFStringify(a));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result">ERROR</div>

wish it will be helpful.

OR

we can use escape(result) in the javascript client side. And use Microsoft.JScript.GlobalObject.unescape(data) on the server side

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜