ASP使用ajax來傳遞中文參數的編碼處理

背景

asp的初版是0.9測試版,自從1996年ASP1.0誕生,迄今20餘載。雖然asp在Windows2000 IIS服務5.0所附帶的ASP 3.0發佈後好像再沒有更新過了,可是因爲其入手簡單,自然的asp+access零配置,在零幾年火的就像如今的微信小程序。雖然時過境遷,其至今在國內還有小規模的用戶市場。php

據工信部發布消息,截止到2018年末,中國中小企業的數量已經超過了3000萬家,個體工商戶數量超過7000萬戶。中小企業信息化廣泛面臨資金短缺、信息人才不足等問題,爲了下降成本,故而企業網站通常外包給其餘小規模的網絡公司製做、維護,網絡公司通常是套站、仿站。操做系統通常使用Windows,web程序搭建上,通常使用php+MySQL或者asp+access的「黃金」組合。html

實踐

這不,筆者最近又用上了asp+access,不過不是企業站的,而是一個小型的教學管理系統。前端

開發過程當中碰到一個問題,asp頁面先後端都是用的gb2312編碼,前端使用ajax來傳遞中文參數,後端進行編碼處理後,以避免存入access爲亂碼。web

注:建議先後端都是用utf-8編碼,就沒下面這麼麻煩了。ajax

前端代碼:小程序

$.post("result.asp?act=update",{
  val: escape(val)
},function(result){
  result = JSON.parse(result);
});

後端代碼:後端

Dim val
val = Trim(vbsUnEscape(request.form("val")))

ECMAScript v3 已從標準中刪除了 escape() 函數和 unescape() 函數,並反對使用它們,所以應該使用 decodeURI() 和 decodeURIComponent() 取而代之。微信小程序

… All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. …
… Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. …
微信

https://www-archive.mozilla.org/js/language/E262-3.pdf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape
Function vbsUnEscape(str)
    Dim i,s,c
    s=""
    For i=1 to Len(str)
        c=Mid(str,i,1)
        If Mid(str,i,2)="%u" and i<=Len(str)-5 Then
            If IsNumeric("&H" & Mid(str,i+2,4)) Then
                s=s & CHRW(CInt("&H" & Mid(str,i+2,4)))
                i=i+5
            Else
                s=s & c
            End If
        ElseIf c="%" and i<=Len(str)-2 Then
            If IsNumeric("&H" & Mid(str,i+1,2)) Then
                s=s & CHRW(CInt("&H" & Mid(str,i+1,2)))
                i=i+2
            Else
                s=s & c
            End If
        Else
            s=s & c
        End If
    Next
    vbsUnEscape=s
End Function

另附上通常這裏用不上的vbsEscape:網絡

'escape時不變的7個符號: *(42) +(43) -(45) .(46) /(47) @(64) _(95)
Function vbsEscape(str)
    dim i,s,c,a
    s=""
    For i=1 to Len(str)
        c=Mid(str,i,1)
        a=ASCW(c)
        If (a>=48 and a<=57) or (a>=65 and a<=90) or (a>=97 and a<=122) Then
            s = s & c
        ElseIf InStr("@*_+-./",c)>0 Then
            s = s & c
        ElseIf a>0 and a<16 Then
            s = s & "%0" & Hex(a)
        ElseIf a>=16 and a<256 Then
            s = s & "%" & Hex(a)
        Else
            s = s & "%u" & Hex(a)
        End If
    Next
    vbsEscape = s
End Function

若是,在前端使用的是encodeURI/encodeURIComponent,而不是escape,那麼,後端還能夠像下面這樣寫:

<%
Function strDecode(str)
    Dim objScript
    Set objScript = Server.CreateObject("ScriptControl")
    objScript.Language = "JavaScript"
    strDecode = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
    Set objScript = NOTHING
End Function
%>

上面的vbsUnEscape函數也能夠這麼寫!

原文:https://xushanxiang.com/2019/11/asp-ajax-escape.html

相關文章
相關標籤/搜索