开发者

DOJO xhrGet json with special character

I use dojo and struts 1.3.8 and I want to pass some "special" characters like è,°,ù,€ via dojo.xhrGet to an action struts saveBill. But when I print the json in the action it gave me

è la prova n°10

I don't know where is the problem, I set all content-type to utf-8 in all the jsp... I used also a struts filter for encoding in utf-8... nothing... where I wrong? This is the javascript code

var billJson = {"row":"0","descr":"è la prova n°10"};
dojo.xhrGet({
    url: "saveBill.do",
    headers: {'bill': billJson, 'Content-Type': 'application/json; charset=UTF-8'},
    handleAs: "text",
    load: function(response, ioArgs)开发者_如何学Python {
            showMessage(response);
    },
    error: function(message, ioArgs) {
            showMessage(message);
    }
});

and this is the response header (copy&paste from Firebug)

Host    localhost:9080
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8
Accept-Language it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset  UTF-8,*
Keep-Alive  115
Connection  keep-alive
bill    {"row":"0","descr":"è la prova n°10"}
Content-Type    application/json; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer http://localhost:9080/CA3_S_001/login.do
Cookie  invoiceTreeSaveStateCookie=undefined%2C000001%2C000014; JSESSIONID=0000QeyArD4K7CDr_oyNkrpw9Zk:-1

Thanks!!!


you need to unicode escape those characters: so instead of è, you'd have \\u00E8

here's a resource: http://www.fileformat.info/info/unicode/category/Ll/list.htm


Sending JSON in an HTTP header is fairly non-standard, and I wouldn't recommend it. One reason not to send JSON in a header is that, as you've found out, HTTP headers are just bytes; they have no intrinsic code page to translate them into strings. I would instead send the JSON data in a POST body; I think you'll have much, much better luck.

However, if you absolutely must send non-ASCII JSON data in a header, you can try calling ServletRequest.setCharacterEncoding("UTF-8"). I think it only affects the parsing of URL parameters and POST bodies, but it's worth a try. You could also, as Robot Woods suggests, \uXXXX encode all non-ASCII characters in the JSON (where XXXX is the hex representation of the UTF-16 encoding of the character).

But seriously, just put it in the POST body; it's a stronger, better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜