zoukankan      html  css  js  c++  java
  • ASP使用ajax来传递中文参数的编码处理

    背景

    asp的第一版是0.9测试版,自从1996年ASP1.0诞生,迄今20余载。虽然asp在Windows2000 IIS服务5.0所附带的ASP 3.0发布后好像再没有更新过了,但是由于其入手简单,天然的asp+access零配置,在零几年火的就像现在的微信小程序。虽然时过境迁,其至今在国内还有小规模的用户市场。

    据工信部发布消息,截止到2018年底,中国中小企业的数量已经超过了3000万家,个体工商户数量超过7000万户。中小企业信息化普遍面临资金短缺、信息人才不足等问题,为了降低成本,故而企业网站一般外包给其他小规模的网络公司制作、维护,网络公司一般是套站、仿站。操作系统一般使用Windows,web程序搭建上,一般使用php+MySQL或者asp+access的“黄金”组合。

    实践

    这不,笔者最近又用上了asp+access,不过不是企业站的,而是一个小型的教学管理系统。

    开发过程中碰到一个问题,asp页面前后端都是用的gb2312编码,前端使用ajax来传递中文参数,后端进行编码处理后,以免存入access为乱码。

    注:建议前后端都是用utf-8编码,就没下面这么麻烦了。

    前端代码:

    $.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

  • 相关阅读:
    IOS系统下虚拟键盘遮挡文本框问题的解决
    ubuntu git的安装更新及配置
    js 画布与图片的相互转化(canvas与img)
    js 图片与base64互相转换
    PHP base64数据与图片的互相转换
    js 判断当前操作系统是ios还是android还是电脑端
    ubuntu下nodejs和npm的安装及升级
    vue中使用html2canvas及解决html2canvas截屏图片模糊问题
    vue文件中引入外部js
    php 执行 命令行命令
  • 原文地址:https://www.cnblogs.com/xusx2014/p/11940441.html
Copyright © 2011-2022 走看看