Ajax字符编码,奇怪的是进入SQL

时间:2014-09-19 19:07:40

标签: jquery sql ajax post character-encoding

我有2个asp经典页面,两者都做同样的事情。取弦并将它们放入数据库中。

Test1.asp:

<%@Language=VBScript %>
<%OPTION EXPLICIT%>


<!-- #INCLUDE FILE="../G_FILES/globals.asp"-->
<!-- #INCLUDE FILE="../g_files/validation.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="<%=gStyle%>">
<script language="javascript" src="../js_files/jquery/jquery-1.10.2.min.js"></script>
<script src="../js_files/jquery/jquery.limit-1.2.source.js"></script>
<script type="text/javascript" src="../js_files/webdb_helper.js"></script>
<script language="javascript">


    $(document).ready(function () {
        //CHARACTER COUNT/LIMITATION TEXTAREA
        $('#body').limit('3000', '#charLeft');
    });




    //Save Message
    $(document).ready(function() {
        $("#saveBtn").click(function() {

            var subject = $('#subject').val();
            var body = $('#body').val();


            var all_good = true;
            //NO ERRORS? INSERT INTO DB


            if (all_good) {
                $.ajax({
                    type: 'POST',
                    url: "edit_template_test.asp",
                    data: {
                        subject: subject,
                        body: body
                    },
                    async: false,
                    cache: false,
                    error: function(jqXHR, textStatus) {
                        alert("Request failed: " + textStatus);
                        var error = jqXHR.responseText;
                        alert(error);
                    }
                }).done(function(response) {
                    //reload message list
                    alert(response);
                });
            }

        });
    });

</script>
</head>

<body style ="width:700px">
<center class="BodyTextBoldDoubleTitle">Service Provider Communications</center><br />

<table class="BodyTextBoldSingle">

    <tr>
        <td>Email Subject:</td>
        <td colspan="4"><input type="text" size="91"  maxlength='120' id="subject" value =""/></td>
    </tr>   

    <tr><td colspan="5">&nbsp;</td></tr>
    <tr>
        <td colspan="5">Body:</td>
    </tr>
    <tr>
        <td colspan="5"><textarea id='body' rows="13" cols="80"></textarea></td>
    </tr>
    <tr>
        <td colspan="4">You have <font id="charLeft">0</font>/3000 characters left.</td>

    </tr>

    <table class="BodyTextBoldSingle">
        <tr>

            <td>&nbsp;&nbsp;&nbsp;</td>
            <td><div id="saveBtn" style="border: 2px solid; border-radius: 40px 40px; background-color: #C0C0C0; height: 25px; width:155px; text-align: center; vertical-align: middle; line-height: 25px; cursor: pointer" ><font class="BodyTextBoldSingle">Save</font></div></td>
        </tr>
    </table>
</center>
</body>
</html>

这通过ajax调用edit_template_test.asp:

<%@Language=VBScript %>
<%OPTION EXPLICIT%>
<!-- #include file="../g_files/globals.asp" -->
<!-- #INCLUDE FILE="../g_files/validation.asp"-->

<%

    dim subject,body

    subject = Request.Form("subject")
    body = Request.Form("body")


        dim db, sql
        set db = Server.CreateObject("Commands.DB")
        sql = "update folder_log set subject = ' " & subject & "', note = '" & body & "' where msg_id       = '00D8EE44-197D-4F95-938E-887005FCE0D6'"
        db.RunCMD g_Conn, sql
        Response.Write subject&"^"&body


    %>

Test2.asp:

<%@Language=VBScript %>
<%OPTION EXPLICIT%>

<!-- #INCLUDE FILE="../G_FILES/globals.asp"-->
<!-- #INCLUDE FILE="../g_files/validation.asp"-->
<%

dim body1, subject1
Dim test_send
    test_send= trim(request.Form("test_send"))
if test_send = "yes" then
    Response.Write "hello"
    body1 = request.Form("body2")
    subject1 = request.Form("subject2")

    dim db, sql
    set db = Server.CreateObject("Commands.DB")
    sql = "update folder_log set subject = ' " & subject1 & "', note = '" & body1 & "' where msg_id = '7E59E4EE-6682-4C74-BD65-9F0244742BC5'"
    db.RunCMD g_Conn, sql


end if

 %>
<html>
<head>

<title></title>
<link rel="stylesheet" type="text/css" href="<%=gStyle%>">
<script language="javascript" src="../js_files/jquery/jquery-1.10.2.min.js"></script>
<script src="../js_files/jquery/jquery.limit-1.2.source.js"></script>
<script type="text/javascript" src="../js_files/webdb_helper.js"></script>
<script language="javascript">


</script>
</head>

<body style ="width:700px">
<form name="test2" id="test2" method="post" action="test2.asp" >
<center class="BodyTextBoldDoubleTitle">Service Provider Communications</center><br />

<table class="BodyTextBoldSingle">

    <tr>
        <td>Email Subject:</td>
        <td colspan="4"><input type="text" size="91" name="subject2" maxlength='120' id="subject2" value =""/></td>
    </tr>   

    <tr><td colspan="5">&nbsp;</td></tr>
    <tr>
        <td colspan="5">Body:</td>
    </tr>
    <tr>
        <td colspan="5"><textarea name="body2" id="body2" rows="13" cols="80"></textarea></td>
    </tr>
    <tr>
        <td colspan="4">You have <font id="charLeft">0</font>/3000 characters left.</td>

    </tr>

       <tr>

            <td>&nbsp;&nbsp;&nbsp;</td>
            <input type="hidden" name="test_send" value="yes" ID="test_send">
            <td><input type="submit" name = "Save"  /></td>
        </tr>
    </table>
</center>
</form>
</body>
</html>

除了一件事,Test1和Test2都会产生相同的预期结果(进入DB)。

当从包含Word不同字符之一的Word(例如TEST - “TEST”)复制并粘贴字符串时,Test2.asp(回发)工作正常,但Test1.asp会将时髦的字符放入数据库(TEST - “TEST” - &gt; TEST - “TEST”)。

有谁知道为什么回帖会正常工作但是ajax路由有编码问题?

我尝试过各种解决方案让它发挥作用。即:

contentType: 'Content-type: text/plain; charset=iso-8859-1',
                    beforeSend: function(jqXHR) {
                        jqXHR.overrideMimeType('text/html;charset=iso-8859-1');
                    },

和其他类型的编码/解码方法。我真的想让ajax方法起作用。

有人得到解释吗?

如果相关,则sql server中的char编码为SQL_Latin1_General_CP1_CI_AS。

4 个答案:

答案 0 :(得分:1)

这是由于字符编码。确保您已使用UTF-8编码保存所有.ASP文件(在例如记事本中打开它们,选择文件&gt;另存为并在另存为对话框中选择UTF-8编码)。

此外,您应该更改“edit_template_test.asp”中的代码,以摆脱任何令人讨厌的SQL注入(想象一下,如果您在主题参数中发送了'; DROP TABLE ... - )。做这样的事情:

dim subject,body

subject = Request.Form("subject")
body = Request.Form("body")

dim db, sql
set db = Server.CreateObject("Commands.DB")
sql = "update folder_log set subject = ' " & Replace(subject,"'","''") & "', note = '" & Replace(body,"'","''") & "' where msg_id       = '00D8EE44-197D-4F95-938E-887005FCE0D6'"
db.RunCMD g_Conn, sql
Response.Write Server.HtmlEncode(subject&"^"&body)

无论如何,以UTF-8编码保存文件应该切换所有浏览器以UTF-8渲染和发布数据。如果没有,你可能需要为ASP经典做这样的事情:Classic ASP's Request.Form is dropping an 8-bit character -- is there a simple way to prevent this?

祝你好运!

答案 1 :(得分:0)

尝试从Test1.asp中删除DOCTYPE声明          

另外         SQL_Latin1_General_CP1_CI_AS 只是SQL服务器的collation property(默认值:ASCII)

答案 2 :(得分:0)

javascript因此ajax字符串的标准编码始终 utf8
所以在将字符串传递给ajax函数之前,请务必将它们转换为utf8ajax接收时,请注意您已收到utf8个编码字符串,然后将其转换为您想要的任何编码。

答案 3 :(得分:0)

尝试添加

<meta charset="UTF-8">

在你的html文件的头上

为什么选择UTF-8:

  

UTF-8支持的字符多于ASCII,允许国际化   要表示的语言。