admin管理员组文章数量:1190804
I have a javascript method which is receiving a UTF-8 encoded string (ViewBag.errorText), and uses this as a parameter to a new function.
The problem is that the text displayed in show_error_dialog
is displaying the html escaped characters (æø
etc') and not the intended ("æåø" etc.).
I presume the problem is the enclosed <text>
tags but can't seem to get around this.
<script type="text/javascript" charset="utf-8">
function performLoadOperations() {
@if(ViewBag.errorText!= null) {
<text>show_error_dialog('@ViewBag.errorText');</text>
}
}
</script>
I have a javascript method which is receiving a UTF-8 encoded string (ViewBag.errorText), and uses this as a parameter to a new function.
The problem is that the text displayed in show_error_dialog
is displaying the html escaped characters (æø
etc') and not the intended ("æåø" etc.).
I presume the problem is the enclosed <text>
tags but can't seem to get around this.
<script type="text/javascript" charset="utf-8">
function performLoadOperations() {
@if(ViewBag.errorText!= null) {
<text>show_error_dialog('@ViewBag.errorText');</text>
}
}
</script>
Share
Improve this question
asked Jun 9, 2011 at 10:12
user204884user204884
1,7952 gold badges19 silver badges27 bronze badges
4 Answers
Reset to default 19I think all Razor-inserted text is HTML-encoded by default. Use Html.Raw()
to pass the string unencoded.
<script type="text/javascript" charset="utf-8">
function performLoadOperations() {
@if(ViewBag.errorText!= null) {
<text>show_error_dialog('@Html.Raw(ViewBag.errorText)');</text>
}
}
</script>
Use: @Html.Raw(Ajax.JavaScriptStringEncode(Model))
to safely insert values into javascript
just use javascript escape function:
function encode_utf8( s )
{
return unescape( encodeURIComponent( s ) );
}
function decode_utf8( s )
{
return decodeURIComponent( escape( s ) );
}
I'm not sure but i think there was unescape() function with js. Try to pass your text with it. It might help
本文标签: aspnet mvc 3Wrong text encoding in string sent to javascriptStack Overflow
版权声明:本文标题:asp.net mvc 3 - Wrong text encoding in string sent to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738430938a2086398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论