ajax提交中文乱码处理

163 阅读1分钟

1、对中文字符串进行编码:

stra='中文字符串'

stra=encodeURI(stra)

python:

# 把utf-8的url编码解码成中文字符
    try:
        if '%25' in args_value:
            args_value = urllib.parse.unquote(args_value.replace('%25', '%'))
        else:
            args_value = urllib.parse.unquote(args_value)
    except:
        pass

    # 替换特殊的空字符
    args_value = args_value.replace(u'\xa0', u'')
    # 是否字符串两端去空格
    if is_strip:
        args_value = args_value.strip()

常用类web_helper.py中已集成了此方法,不用再单独处理。

java:

decodeURI()方法相当于java.net.URLDecoder.decode(URIString, "UTF-8"); encodeURI()方法相当于java.net.URLEncoder.encode(URIString, "UTF-8");

2、ajax提交的参数设置:

增加contentType: $.ajax({ url:'', type:'post', dataType:'json', contentType: "application/x-www-form-urlencoded; charset=utf-8", success:show() });