3

I need to manage char like ' in my JSONP request, trought Ajax by jquery. So (from C#) this is what I've done :

myText = "Hello I'm a string";
myText.Replace("'", "\'");
Response.Write(Request["callback"] + "({ htmlModulo: '" + myText + "'});");

but on Client side it broke :

parsererror - SyntaxError: missing } after property list

so, How can I manage ' if the replace doesnt works?

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

6

Serializing JSON is already solved for you by .NET. Use System.Web.Script.Serialization.JavaScriptSerializer:

var serializer = new JavaScriptSerializer();

To construct JSONP you just need to concatenate prefix (either hardcoded or passed in "callback" query parameter) and suffix (normally just ), but sometimes you may need to pass extra parameters depending on what caller expect like ,42, null) ) with JSON text.

Sample below shows constructing JSONP based on "callback" parameter, replace value of jsonpPrefix based on your needs.

var myText = "Hello I'm a string";
var jsonpPrefix = Request["callback"]  + "(";
var jsonpSuffix = ")";
var jsonp = 
    jsonpPrefix + 
    serializer.Serialize(new {htmlModulo = myText}) + 
    jsonpSuffix);
Response.Write(jsonp);

You should always use a serializer, because doing it yourself means you're more likely to mess up and violate the JSON spec. For example, you are not quoting the key, which is required by JSON, so jQuery and other JSON parsers will be very confused. It will handle all characters that need to be escaped, as well.

More on constructing JSON can be found in How to create JSON string in C#.

Community
  • 1
  • 1
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
  • 2
    @markzzz JSON is very strict about what is allowed. Part of the point of JSON is that it is a data format, so you aren't supposed to put arbitrary code in it. Also "\u003cdiv" is exactly the same thing as "
    – Stefan Rusek Feb 24 '12 at 14:13
2

leave the text as it is, but use double quotes to escape it in json:

Response.Write(Request["callback"] + "({ htmlModulo: \"" + myText + "\"});");

you could also try this one:

var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = jsonSerializer.Serialize(yourCustomObject);

and then i leave this as well

mindandmedia
  • 6,800
  • 1
  • 24
  • 33
-1

You would need to do:

myText.Replace("'", "\\'");

If you want an escaped single quote in the value of your htmlModulo property. However, your actual JSON would still be invalid. According to the specification, you should be using double quotes to surround key names and values. Thus, for your response to be valid, you would need to send:

{"htmlModulo": "myText value"}

It really shouldn't matter with JSONP since that relies on inserting a new script element into the DOM. Thus even the improper JSON would be interpreted correctly by the JavaScript interpreter. The problem is if you ever request the data as plain JSON. If you do that, then the way you are sending it now will fail with a variety of libraries. In particular, jQuery will fail silently with improper JSON.

James Sumners
  • 14,485
  • 10
  • 59
  • 77
  • With `.Replace("'", "\\'");` it also fails on client side : `parsererror - SyntaxError: missing } after property list` – markzzz Feb 24 '12 at 13:54
  • Without some example text being sent by your script, that's the best I can suggest. – James Sumners Feb 24 '12 at 13:56
  • With `myText = "Hello I'm a string"; myText.Replace("'", "\\'");` on client side I get `jQuery17109806225378866104_1330091829763({ htmlModulo: 'Hello I'm a string'});` – markzzz Feb 24 '12 at 13:58
  • Is that the result copied and pasted from a web inspector? Or is that the raw data being served by the web server? If it's from a web inspector then the control sequences are not going to be shown. – James Sumners Feb 24 '12 at 14:59