tags:

views:

220

answers:

3

I'm sending back a bunch of image tags via JSON in my .ashx response.

I am not sure how to format this so that the string comes back with real tags. I tried to HtmlEncode and that sort of fixed it but then I ended up with this stupid \u003c crap:

["\u003cimg src=\"http://www.sss.com/image/65.jpg\" alt=\"\"\u003e\u003c/li\u003e","\u003cimg src=\"http://www.xxx.com/image/61.jpg\" alt=\"\"\u003e\u003c/li\u003e"]

What the heck is \u003c ?

here's my code that created the JSON for response to my .ashx:

private void GetProductsJSON(HttpContext context) { context.Response.ContentType = "text/plain"; int i = 1;

...do some more stuff

foreach(Product p in products)
{
    string imageTag = string.Format(@"<img src=""{0}"" alt=""""></li>", WebUtil.ImageUrl(p.Image, false));

    images.Add(imageTag);
    i++;
}

string jsonString = images.ToJSON();
context.Response.Write(HttpUtility.HtmlEncode(jsonString));

}

the toJSON is simply using the helper method outlined here:

http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx

+1  A: 

You are returning JSON array. Once parsed using eval("("+returnValue+")") it is in readily usable condition.

EDIT: This code is from jquery.json.js file:

var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
var meta = {    // table of character substitutions
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };

$.quoteString = function(string)
// Places quotes around a string, inteligently.
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
{
    if (escapeable.test(string))
    {
        return '"' + string.replace(escapeable, function (a) 
        {
            var c = meta[a];
            if (typeof c === 'string') {
                return c;
            }
            c = a.charCodeAt();
            return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
        }) + '"';
    }
    return '"' + string + '"';
};

Hope this gives you some direction to go ahead.

TheVillageIdiot
so you're saying having \u003c is normal and eval will convert that to a < or > ?
CoffeeAddict
i still don't get it and what \u003c is
CoffeeAddict
I've tried (returning array containing array of img tags. But when I get proper HTML { var ar=response.d;} and ar contains array of images
TheVillageIdiot
thanks, so the jQuery parser will automatically convert them.
CoffeeAddict
+1  A: 

\u003c is an escaped less-than character in unicode (Unicode character 0x003C).

The AJAX response is fine. When that string is written to the DOM, it will show up as a normal "<" character.

richardtallent
but then why does this not show any of those strange unicode characters:http://api.flickr.com/services/feeds/photos_public.gne?format=json
CoffeeAddict
notice how the description has valid html and none of those \u00
CoffeeAddict
ok, then in that case check out this url:http://api.flickr.com/services/feeds/photos_public.gne?format=jsonwhy does it show up so nicely and with no unicode crap? View source and check it out. It's using that url above to get the data and then uses .description which includes some HTML already.
CoffeeAddict
You don't *have* to escape < and > when sending pure JavaScript response back to the browser (as the Flickr API is doing).But if you call toJSON() to write JavaScript in the middle of a <script> block in an HTML page, and the script block doesn't embed the JavaScript in an HTML comment ("<!-- //code here -->"), escaping the < and > characters is necessary.So the JsonSerializer escapes the < and > since it doesn't "know" where it is being called from. It uses Unicode escaping rather than HTML entities (< / >) because the Unicode escapes will be interpreted properly in both cases.
richardtallent
A: 

all you need to do is to use javascript eval function to get a pure HTML (XML) markup on the front end.

i.e. in a ajax call to a webservice, this can be the success handler of tha call, the service returns a complex html element:

... success: function(msg) {$(divToBeWorkedOn).html(eval(msg**)**);alert(eval(msg));}, ...

good luck

Masoud