JSONP string with scrambled non-ASCII characters

I have a small web service in C# 4.0 which returns data via JSONP. The problem I have is that non-ASCII characters - like the German umlauts (ä, ö, ü) - are scrambled when they arrive in the client. To following simplified code illustrates this:

// The interface
[WebGet( ResponseFormat = WebMessageFormat.Json )]
string GetTestString();


// The implementation
public string GetTestString()
    return "String with some German characters: äöüß";

Now if I access the web method through Firefox by entering the URL http://localhost:31843/TestWebService/GetTestString I get the correct result:

"String with some German characters: äöüß"

However if I specify the callback parameter in the URL (http://localhost:31843/TestWebService/GetTestString?callback=Test), which is what JSONP does, I get a scrambled result:

Test("String with some German characters: äöüß");

The JSONP serializer is that of the Microsoft .NET 4.0 framework which I activated by setting webHttpBinding.CrossDomainScriptAccessEnabled = true; in my code. I didn't find any way to influence the character encoding of that serializer. Do you have any ideas?

Another hint: After the web request for the JSONP string with the scrambled characters, Firebug shows the following error on the console:

"The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature."

Obviously this seems to be the problem. But the question is, how to configure the correct encoding (and why isn't it UTF8 by default)?


I think the Microsoft .NET DataContractJsonSerializer has a bug. In the first case with JSON, it sends the following HTTP header:

Content-Type: application/json; charset=utf-8

When using JSONP by specifiying the callback URL parameter, it sends:

Content-Type: application/x-javascript

So no character encoding is specified, leaving the decision of the character set up to the browser. After some research in the Internet I'm convinced that there is no (easy) way to force the DataContractJsonSerializer to send out a Content-Type HTTP header with a proper charset.


I have now found a pretty simple solution to this - maybe a bit artificial - problem.

Explanation: I'm developing an app for smartphones with a JavaScript framework, so it's no native app but runs in the smartphone's browser engine. For this reason, all web content (HTML, JavaScript, CSS files, etc.) is loaded locally, not remote from a web server. That's why there's no appropriate Content-Type HTTP header with a charset, because there is no HTTP at all. In the index.html file, which is the main page (the one and only) for the app, there was also no charset specified. So the browser assumed some default charset, unfortunately not UTF-8.

So the simple solution for me was to specify the character set in the index.html by adding the following line:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

That way, also the embedded JSONP data now gets displayed correctly, without requiring the JSONP HTTP response to have its character set specified explicitly.

Of course, this doesn't resolve the incorrect display in Firefox, as stated in my question above. But this doesn't matter, as it only has to be displayed correctly on the smartphone's browser.

Need Your Help

Set the same height 'li's with images

html css

All my 'li' are set to 240w X 260h and inside I have a picture that can be up to 240w X 140H I want the images to be fit inside the div.photo however the picture' size but let's say that I changed my

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.