我正在开发一个通过
xe:djxDataGrid
加载和保存数据的 xe:restService
。它从 Domino 文档加载数据,此时所有斯堪的纳维亚字符(如 ä 和 ö)看起来都正常。
但是,如果数据被修改并保存回服务器,ä 会变成 à,ö 会变成 à¶。我认为这是因为数据是 UTF-8 编码的,但在某些时候被解释为 ISO-8559-1。
我尝试在任何地方使用 UTF-8:
页面的响应标头有以下行:
Content-Type:text/html;charset=utf-8
我还尝试将所有地方的字符集更改为 ISO-8559-1,但这没有帮助。我尝试将 REST 控件的 computeWithForm 属性设置为 true 以查看是否使其使用表单字符集,但没有效果。
在 Firefox (17.0.1) 中一切正常!该问题至少出现在 Chrome 和 IE9 中。
因为它是特定于浏览器的,所以我认为当 Dojo 将数据发送到 REST 服务时它会中断。但我还没有找到一种方法来专门告诉Dojo使用UTF-8。
HTML 标签如下所示:
铬:
<html lang="fi">
火狐浏览器:<html class="dj_gecko dj_contentbox" lang="fi">
djConfig 是这样的:
djConfig="locale: 'fi-fi'
.
Domino 版本是 8.5.3FP3,ExtLib 日期是 20121217。
不幸的是我不能强迫用户只使用 Firefox。有什么想法如何解决这个问题吗?
编辑1
同样的问题出现在 ExtLib 演示应用程序中:xpagesext.nsf/REST_DojoGrid.xsp 和 xpagesext.nsf/REST_DojoGridJsonRest.xsp。
编辑2
作为解决方法,我可以在表单上的输入翻译字段中执行此操作:
@ReplaceSubstring(@ThisValue; "ä":"ö"; "ä":"ö");
当我在 REST 控件中启用 computeWithForm 时,这才有效。我需要包括所有可能使用的非英语字符。或者有没有通用的方法来转换所有?
编辑3
根据@Esailija 的建议,我检查了将数据保存到服务器的 HTTP PUT 请求。这次我用Opera 12.12 进行测试,也有这个问题。 PUT 请求的内容类型为:
Content-Type: application/json
对于 Firefox 来说是:
Content-Type:application/json; charset=UTF-8
这解释了问题,但是如何解决呢?据我了解,问题出在 dojox.grid.DataGrid (1.6) 控件上,它没有在 PUT 请求中设置字符集。 Firefox 似乎会自动设置它。或者实际上是在 ExtLib DataGrid 中无法在 Dojo 控件中设置字符集?我还没有找到在 DataGrid 中设置字符集的方法。
编辑4
尝试将 REST 控件中的 contentType 属性从
application/json
更改为 application/json; charset=UTF-8
。这没有帮助,PUT 的内容类型仍然是 application/json
。
谢谢,
我修改了代码,它的工作
dojo.addOnLoad(
function() {
if( !(dojo._xhrPost )) {
dojo._xhrPost = dojo.xhrPost;
}
dojo.xhrPost = function (args) {
if ( args.headers && ( args.headers["Content-Type"] == "application/json" ) ) {
args.headers["Content-Type"] = "application/json;charset=UTF-8";
}
return dojo._xhrPost(args);
}
}
)
我应该能够通过覆盖 Dojo
charset
并修改 HTTP 标头来修改 xhrPut
。
以下是 Sven Hasselbach 的做法,但这需要进行修改,以便我们不会覆盖现有标头(因为它们由 REST 服务使用),而只需添加/修改它们:
/**
* Cache Prevention for Dojo xhr requests
*
* Adds no-cache header and enables dojo's preventCache feature
* for every dojo xhr call. This prevents the caching of partial
* refreshs.
*
* @author Sven Hasselbach
* @version 0.3
*
**/
dojo.addOnLoad(
function(){
if( !dojo._xhr )
dojo._xhr = dojo.xhr;
dojo.xhr = function(){
try{
var args = arguments[1];
args["preventCache"] = true;
args["headers"] = { "cache-control": "no-cache" };
arguments[1] = args;
}catch(e){}
dojo._xhr( arguments[0], arguments[1], arguments[2] );
}
}
)
http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache-prevention-for-dojo-xhr-requests
我已经有了解决方法,但会在某个时候尝试一下。
最好做到100%有效。
dojo.addOnLoad(function() {
if (!dojo._xhr) {
dojo._xhr = dojo.xhr;
}
dojo.xhr = function(){
try{
if (arguments[0]=="PUT" || arguments[0]=="POST") {
var args = arguments[1];
args["contentType"] += ";charset=UTF-8";
args["preventCache"] = true;
var headers = args["headers"];
headers["Accept"] += ";charset=UTF-8";
headers["Accept-charset"] = "UTF-8";
headers["cache-control"] = "no-cache";
args["headers"] = headers;
arguments[1] = args;
}
} catch(e){}
dojo._xhr(arguments[0], arguments[1], arguments[2]);
}
});