jQuery,经典ASP和SQL Server - 在£之前插入

时间:2014-05-07 15:10:55

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

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

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

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

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

因此,当我进行数据库插入时,它实际上也会插入£,而不仅仅是£

为什么会发生这种情况?

Page 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>

2 个答案:

答案 0 :(得分:1)

  

注意:

     

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

正如我已经解释过,由于您缺少关键步骤而导致编码不匹配,请尝试此操作;

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

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

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

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

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

  3. 发布的任何数据都应该以{{1​​}}的形式发送,这取决于您使用JQuery Ajax Post的情况(默认情况下使用UTF-8),因此不应进行任何更改需要在这里。


  4. 有用的链接

答案 1 :(得分:0)

Response.ContentType = "text/html" 
Response.AddHeader "Content-Type", "text/html;charset=UTF-8" 
Response.CodePage = 65001 
Response.CharSet = "UTF-8"

添加了这个,很好!