views:

311

answers:

2

Hello,

I have an Asp.Net web app which users include a script tag in their web page, and get data from my server (using JsonP & a Generic handler (ashx))

The data is in hebrew, and I set the charset to utf-8 in the response.

When the client web site (which displays the data) uses "windows-1255" I don't see the text properly.

The script can be included in any web page with any character set.

Do I need to convert my data or set the response charset property differently?

Thanks Yaron

+1  A: 

Its probably better that you specify clearly to your clients that your interface uses UTF-8 encoding. When they forward your data down to their clients they should set the Response.CharSet to UTF-8. Of course you are alreadying doing that on your end right?

AnthonyWJones
Yaron
@Yaron: I see, I'm not that familiar with JSONP, Bobince has this covered. I've seen this IE issue before in straight forward non-cross domain javascript scenarios where files are of different encodings.
AnthonyWJones
+2  A: 

When the client web site (which displays the data) uses "windows-1255" I don't see the text properly.

Yeah. Here's the thing: when you are serving <script>, you can set the ‘charset’ parameter in the response's Content-Type to anything you like, but IE will ignore you and use the encoding of the origin page instead. (Maybe some other browsers too.)

So if you're doing JSONP or another script-tag-based means of pulling in data, that data must be either in the same encoding as the origin page, or plain ASCII. If you are providing an external service for many web pages in potentially different encodings, you must stick to ASCII.

That means your JSON encoder must convert all non-ASCII characters in string literals to their JS-escaped form, for example \u20AC instead of a raw €.

bobince
Yaron
You don't need on on the JS side, JS natively understands the \uxxxx encoding. You need something on the C# side to create those encodings
AnthonyWJones
The JS side will just work, `\u`-escapes are a native part of JavaScript string literals. I haven't surveyed all the JSON implementations for .NET, but at a quick glance LitJSON does seem to write non-ASCII as escapes. http://litjson.sourceforge.net/
bobince