JSONP string with scrambled non-ASCII characters

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

// The interface [OperationContract] [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 makes JSONP, I get a scrambled result:

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

The JSONP serializer is a Microsoft.NET 4.0 engine that I activated by setting webHttpBinding.CrossDomainScriptAccessEnabled = true; into your code. I have not found a way to affect the character encoding of this serializer. Do you have any ideas?

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

"The character encoding of a plain text document has not been declared. The document will be displayed with garbled text in some browser configurations if the document contains characters outside the US-ASCII range. You must declare the character encoding of the file in the transfer protocol or file, use the byte byte character as the encoding signature . "

This is obviously a problem. But the question is how to set up the correct encoding (and why is it not UTF8 by default)?

Update:

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

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

When using JSONP, setting the callback URL parameter, it sends:

 Content-Type: application/x-javascript 

Thus, the character encoding is not set, leaving the character solution installed in the browser. After doing some research on the Internet, I am convinced that there is no (simple) way to force the DataContractJsonSerializer to send the Content-Type HTTP header with the appropriate encoding.

+4
source share
1 answer

Now I have found a fairly simple solution - perhaps a little artificial - the problem.

Explanation: I am developing a smartphone application with a JavaScript framework, so it does not have its own application, but works in a smartphone browser. For this reason, all web content (HTML, JavaScript, CSS files, etc.) is downloaded locally and not remotely from the web server. Therefore, there is no suitable Encoding Content-Type HTTP header because HTTP is not available at all. The index.html file, which is the main page (the one and only) for the application, also does not specify the encoding. Thus, the browser accepted some default encodings, unfortunately not UTF-8.

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

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

Thus, now the embedded JSONP data is displayed correctly, without requiring the HTTP JSONP response to explicitly indicate its character set.

Of course, this does not allow incorrect display in Firefox, as stated in my question above. But it does not matter, since it should only be displayed in the smartphone browser.

+3
source

All Articles