【问题标题】:jQuery, Classic ASP and SQL Server - Â being inserted before £jQuery、Classic ASP 和 SQL Server - 在 £ 之前插入
【发布时间】:2014-05-07 15:10:55
【问题描述】:

我正在使用一个相当简单和标准的 jQuery post 将数据发布到另一个页面。两个页面都有

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<% Response.CharSet = "UTF-8" %>

jQuery 帖子的函数部分只有alert(data),而我返回的只是用括号括起来的一个表单字段的值。因此,当我添加英镑符号 (£) 时,它会相应地提醒 [£]

但是,我也在我发布到的页面上写入了一个文本文件,它实际上写入了£ 而不仅仅是£

所以当我进行数据库插入时,它实际上也插入了£ 而不仅仅是£

发生这种情况的任何原因?

第 1 页

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Intelligence Point </title>
    <meta name="description" content="">

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="">

<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="/css/custom.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Shadows+Into+Light' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]--> 
    <style type="text/css">        p,textarea {
            padding: 5px;
        }
        textarea {
        border:1px solid #ccc;}
    </style>
</head>
<body>


    <div class="container" style="margin-top:-30px;">
        <p>&nbsp;</p>

    <p class="bg-primary">Comments last updated by JJ004 on 2014-05-07 at 16:28</p>

    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment1">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment2">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment3">£</textarea></p>
    <p><textarea class="bg-success" maxlength="4000" style="width:100%;" rows="4" wrap="soft" id="comment4">£</textarea></p>

    <button type="button" class="btn btn-success" id="postcomment">Save comments</button>
    </div>


    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>



    <script type="text/javascript">
        $(document).ready(function () {
            window.resizeTo(800, 750);
            $("#postcomment").click(function () {
                $.post(
                   "putComment.asp",
                   {
                       yR: "123",
                       yP: "456",
                       yN: "",
                       yF1: "XXX",
                       yF2: "YYY",
                       yF3: "ZZZ",
                       yF4: "",
                       yC1: $('#comment1').val(),
                       yC2: $('#comment2').val(),
                       yC3: $('#comment3').val(),
                       yC4: $('#comment4').val()
                   },

                   function (data) {

                       //alert(data);
                       var url = "SOMEWHERE.COM";
                       alert("Narrative saved");
                       window.opener.location.href = url;
                       window.close(); // or self.close();

                   }

                );
            });
        });
    </script>

</body>
</html>

putComment.asp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <% Response.CharSet = "UTF-8" %>
</head>
<body>

<!--#include virtual="/comments/connProd.inc"-->
<%
Function cleanSQL(thisWord)
       Dim newWord
       If thisWord <> "" Then
                       newWord = Replace(thisWord,"/*","")
                       newWord = Replace(newWord,"*/","")
                       newWord = Replace(newWord,"UNION","")
                       newWord = Replace(newWord,"'","''")
                       newWord = Replace(newWord,"""","&amp;quot;")
                       newWord = Replace(newWord,"<script>","[script]")
                       newWord = Trim(newWord)
       End If
       cleanSQL = newWord
       End Function

       Function in_array(element, arr)
    For i=0 To Ubound(arr) 
        If Trim(arr(i)) = Trim(element) Then 
            in_array = True
            Exit Function
        Else 
            in_array = False
        End If  
    Next 
End Function

zR=cleanSQL(request.form("yR"))
zP=cleanSQL(request.form("yP"))
zN=cleanSQL(request.form("yN"))

zC1=cleanSQL(request.form("yC1"))
zC2=cleanSQL(request.form("yC2"))
zC3=cleanSQL(request.form("yC3"))
zC4=cleanSQL(request.form("yC4"))

zF1=cleanSQL(request.form("yF1"))
zF2=cleanSQL(request.form("yF2"))
zF3=cleanSQL(request.form("yF3"))
zF4=cleanSQL(request.form("yF4"))

    'response.Write "["&zC1 & "]<br/>"

sql="insert into IPPR.Comments values("
sql=sql+"'"+zR+"',"
sql=sql+"'"+zP+"',"
sql=sql+"'"+zF1+"',"
sql=sql+"'"+zF2+"',"
sql=sql+"'"+zF3+"',"
sql=sql+"'"+zF4+"',"
sql=sql+"'"+zC1+"',"
sql=sql+"'"+zC2+"',"
sql=sql+"'"+zC3+"',"
sql=sql+"'"+zC4+"',"
sql=sql+"getdate(),"
sql=sql+"'"+zN+"')"


dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject") 
set f=fs.OpenTextFile("c:\inetpub\wwwroot\text\log.txt",8,true)
f.WriteLine("")
f.WriteLine("["&zC1 & "]" & sql)
f.close
set f=nothing
set fs=nothing


    'response.write sql
    'response.flush
set rs=conn.execute(sql)

%>
    </body>
</html>

【问题讨论】:

  • 这个已经在本站处理过多次了,你可能会觉得this answer我给的有用。
  • 该字符似乎被插入到 SQL 之前。 nvarchar 和 2012 @Lankymart - 不适合我,仍然不起作用
  • @marc_s OP 说它在进入数据库之前已经不匹配的编码。问题很可能是他们的页面没有保存为UTF-8
  • @pee2pee 我保证它会起作用,我一次又一次地听到 “它不起作用” 关于这个确切的问题,最终它总是会起作用。您能否更新您的问题并显示您尝试过的代码,我会帮助您解决它。 Another link OP 承认他们缺少一些东西。
  • 编辑 - 我很可能错过了一些东西。透过树林看不到树林……

标签: jquery asp-classic character-encoding jquery-post


【解决方案1】:

注意:

comments 中的代码真的很难传达,所以我正在写这篇文章。

正如我所解释的,由于缺少关键步骤而导致编码不匹配,请试试这个;

  1. 将以下行添加到Page 1putComments.asp开头

    <%@Language="VBScript" CodePage = 65001 %>
    <%
    Response.CodePage = 65001
    Response.CharSet = "UTF-8"
    %> 
    
    • 第一行告诉 IIS 将页面内容处理为UTF-8

    • 第二行指示将所有Response 字符串编码为UTF-8

    • 第三行相当于在Content-Type标头中设置;Charset=UTF-8

  2. 确保链中的所有 ASP 文件(包括任何 #include 文件)都以 UTF-8 编码保存,否则 IIS 将文件处理为 UTF-8 时会导致编码不匹配。

  3. 1234563 /p>

有用的链接

【讨论】:

  • 明天会用新鲜的眼光试一试。这是 10 多年来编写经典 ASP 编码的第一次,但是自从 jQuery 出现以来,我遇到了很多这样的问题!引用的任何 CSS 和 JS 文件怎么样?
  • Response.ContentType = "text/html" Response.AddHeader "Content-Type", "text/html;charset=UTF-8" Response.CodePage = 65001 Response.CharSet = "UTF-8 "
  • @pee2pee CSS 和 JS 文件应该与 ASP 返回的 HTML 的编码方式无关。有时只是离开一段时间然后再回来是你能做的最好的事情。
  • 我如何知道我的文件保存为哪个代码页?
  • @IanWarburton 取决于您使用的是什么,如果您使用 Visual Studio 编辑您的文件,您可以使用Advanced Save Options 命令检查(默认情况下不显示) .自定义菜单栏添加,可以在File菜单命令下找到。
【解决方案2】:
Response.ContentType = "text/html" 
Response.AddHeader "Content-Type", "text/html;charset=UTF-8" 
Response.CodePage = 65001 
Response.CharSet = "UTF-8"

添加了这个,很好!

【讨论】:

  • 正如我已经解释过的,Response.CharSet 与在Response.AddHeader("Content-Type", 中设置;charset 相同,Response.ContentType = "text/html" 与设置Response.AddHeader("Content-Type", "text/html 相同。那条线完全无关紧要,那么这与我的答案有何不同?我告诉过你它会起作用的。
  • 我刚刚将前两行添加到所有有问题的文件中,并且它起作用了。我把它们拿出来,它停止工作了。我每个都留了一个,但没有用。我把它们都放回去了,它起作用了。虽然我很感激帮助和指导,但这两条线是拼图的最后一块 - stackoverflow.com/a/7195451/544542
  • 我觉得很奇怪,因为这两个语句与Response.AddHeader("Content-Type", "text/html;charset=UTF-8")相同,但你现在是suggest this works的第二个人。将不得不研究它,但在我处理经典 ASP 编码的这些年里,我从未遇到过这个问题。
  • 这实际上是我 10 多年来第一次遇到编码问题——来自 ASP、ColdFusion、PHP 等
  • 如果您曾经使用过¬ 字符作为字符串分隔符,您现在可能已经遇到过它,那真的很痛苦。
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多