字符串编码问题

发布于 2024-08-18 18:05:58 字数 986 浏览 3 评论 0原文

我在字符串存储在数据库中的方式上遇到了一个非常奇怪的问题,因此,我在 Javascript 中收到了这些“未终止的字符串文字”错误。

以下是我正在做的事情的概述:

平台:C#/ASP.NET MVC 1.0、SQL Server 2005、SparkViewEngine、YUI 2

在我看来,我使用 NewtonSoft 的 Json.NET 将对象序列化为 JSON 数据结构。

<script type="text/javascript">
    // <![CDATA[ 
    var data = YAHOO.lang.JSON.parse("${Newtonsoft.Json.JsonConvert.Serialize(Model)}");
    ....
</script>

通常,这是可行的,但我注意到我从数据库中提取的字段之一包含以下数据,这导致字符串无法正确形成。

数据库字段是NVARCHAR(2000)

对于某些条目,当我从数据库复制并粘贴到记事本时,我在字符串中得到了这些奇怪的字符。

编译?分析?& ?建议? 在Firebug

中,它显示为一堆换行符:

Analysis &建议甲板","StartDate":"1/19/10","FinishDate":"1/26/10","Duration":6.0,"

编译分析和建议 推荐平台

更新 在与用户交谈后,我发现他们正在使用从 Word 文档复制并粘贴到 HTML 表单的方法。

表单本身使用 YUI 连接管理器进行异步 POST 调用 (AJAX) 来保存表单值。 数据库保存表单字段值以及与其关联的任何编码。

Word 中似乎有可打印的字符,但 ASCII 中却没有。有什么方法可以检测到这一点并正确编码吗?

I am having a very weird issue in the way strings get stored in my database, and as a result, I am getting these "unterminated string literal" errors in Javascript.

Here's an overview of what I am doing:

Platform: C#/ASP.NET MVC 1.0, SQL Server 2005, SparkViewEngine, YUI 2

In my view, I serialize an object into a JSON data structure using Json.NET from NewtonSoft.

<script type="text/javascript">
    // <![CDATA[ 
    var data = YAHOO.lang.JSON.parse("${Newtonsoft.Json.JsonConvert.Serialize(Model)}");
    ....
</script>

Normally, this works, but I noticed that one of the fields I pull from the database contains the following data, which causes the string to not be formed properly.

The database field is an NVARCHAR(2000).

For some of the entries, I get these weird characters in the string when I copy and paste from the database to notepad.

Compile ?Analysis ?& ?Recommendations? deck

In Firebug, it shows as a bunch of line breaks:

Analysis & Recommendations deck","StartDate":"1/19/10","FinishDate":"1/26/10","Duration":6.0,"

Compile 
Analysis 
& 
Recommendations
 deck

UPDATE
After talking to the user, I discovered that they were using the copying and pasting from a word document onto the HTML form.

The form itself uses the YUI Connection Manager to make an asynchronous POST call (AJAX) to save the form values.
The database saved the form field value along with any encoding associated with it.

It seems like there are characters in Word that are printable, but not in ASCII. Is there any way to detect this and encode correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2024-08-25 18:05:58

这似乎是您没有正确处理引号字符的情况。你有好几种方法可以走。我为我们的网站添加注释的方式是确保所有引号(单引号和双引号)都经过编码,以便您的 SQL、ASPX 和 SQL 都可以使用。 JavaScript 不会打嗝。
我们的方法适用于客户评论字段,因此我们在转换方面做得太过分了。

Function SafeComment(ByVal strInput)
' Renders Any Comment Codes Harmless And Leaves Them HTML readable In An eMail Or Web Page
' Try: SafeComment("`~!@#$%^&*()_+=-{}][|\'"";:<>?/.,")
    SafeComment = ""
    If Len(strInput) = 0 Then Exit Function
    SafeComment =   Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                        Server.HtmlEncode(Trim(strInput)), _
                    "-", "-"), ":", ":"), "|", "|"), _
                    "`", "`"), "(", "("), ")", ")"), _
                    "%", "%"), "^", "^"), """", """), _
                    "/", "/"), "*", "*"), "'", "'")
End Function

它并不漂亮,但它可以完成工作。

This appears to be a case of you not handling the quote characters properly. You have several ways to go. The way I use for comments for our web site is to make sure all quotes are encoded, both single and double, so that both your SQL, ASPX & Javascript won't hiccup.
Our method is for customer comment fields so we're overboard on the conversions.

Function SafeComment(ByVal strInput)
' Renders Any Comment Codes Harmless And Leaves Them HTML readable In An eMail Or Web Page
' Try: SafeComment("`~!@#$%^&*()_+=-{}][|\'"";:<>?/.,")
    SafeComment = ""
    If Len(strInput) = 0 Then Exit Function
    SafeComment =   Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                    Replace(Replace(Replace( _
                        Server.HtmlEncode(Trim(strInput)), _
                    "-", "-"), ":", ":"), "|", "|"), _
                    "`", "`"), "(", "("), ")", ")"), _
                    "%", "%"), "^", "^"), """", """), _
                    "/", "/"), "*", "*"), "'", "'")
End Function

It's not pretty, but it does the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文