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:
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.