我使用 grails 框架进行了以下操作:
def showText () {
def myInstace = User.get(params.id)
def myText = myService.getText(myInstance.id)
render (text: myText, encoding: "UTF-8", contentType: "text/plain")
}
上面的方法工作正常,因为当我尝试使用 cUrl 查看此方法返回的数据时,我得到了可以接受的内容:
来自 cUrl
The (<b>quick</b>) [brown] {fox} jumps!
Over the $43,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der ,,schnelle” braune Fuchs springt
iiber den faulen Hund. Le renard brun
<<rapide» saute par-dessus le chien
v ‘$5’ paresseux. La volpe marrone rapida
“TATVPKOY ‘~35 salta sopra_1l cane pigro. El zorro
“ marron répido salta sobre el perro
perezoso. A raposa marrom rzipida
salta sobre o e50 preguicoso.
但是,当它加载到模态框上时,看起来像这样:
The (quick) [brown] {fox} jumps! Over the $43,456.78 #90 dog & duck/goose,
as 12.5% of E-mail from aspammer@website.com is spam. Der ,,schnelle”
braune Fuchs springt iiber den faulen Hund. Le renard brun <
我的 jQuery 代码很简单:
$('a[id^="myText"]').click (function () {
$.post($(this).data('url'), function (data){
$("#msg").html(data);
});
$('#showText').modal()
});
所以,我似乎无法理解我做错了什么?上面的内容被加载到一个 div 中,该 div 显示为模式框。此外,我的 config.groovy 具有 UTF-8 设置:
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
最佳答案
您的文本会自动转换为html格式,即<b>, <lazy>, <rapide>
作为 html 标签,当 <<rapide
渲染后,存在格式错误。
解决方案是使用 StringEscapeUtils.unescapeHtml 方法修改 Controller 中的渲染方法,如下:
render (text: StringEscapeUtils.unescapeHtml(myText), encoding: "UTF-8", contentType: "text/plain")
关于java - 使用 jQuery .Post 将 UTF-8 作为文本渲染到模态框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250699/