Python中ajax post提交邮箱地址后@符号被转义了怎么办?

Rt.有什么途径可以避免 @被转义呢。不改 flask 程序代码


	data['cemail'] = $('input[name="cemail"]').val();       
    var $result = $('#result');
       $.ajax({
           url:'/tpush',
           data: data,
           type: 'POST',
           contentType: "application/json",
           dataType:'json',
           success:function (data) {
               if (!data.r){
                   $result.html(data.rs)
               }else{
                   $result.html(data.error)
               }
           }
       });

结果:cname=66655555555&cemail=root%40doge.net&curl=59999999999&text=666666 怎么处理呢 ajax 呢


Python中ajax post提交邮箱地址后@符号被转义了怎么办?

9 回复

不用处理,后端接收到的是 @


这个问题通常是因为数据在传输过程中被错误地编码了。最常见的情况是前端使用JSON.stringify()处理数据时,或者后端框架自动进行了URL编码。

解决方案:

  1. 前端检查:确保发送的是正确的JSON格式,而不是表单编码
// 正确的方式 - 发送JSON
$.ajax({
    type: "POST",
    url: "/your-endpoint",
    contentType: "application/json",
    data: JSON.stringify({email: "user@example.com"}),
    success: function(response) {
        console.log(response);
    }
});
  1. 后端处理:在Python中正确解析数据
from flask import Flask, request, jsonify
import json

app = Flask(__name__)

@app.route('/submit-email', methods=['POST'])
def submit_email():
    # 方法1:如果前端发送的是JSON
    if request.is_json:
        data = request.get_json()
        email = data.get('email')
    # 方法2:如果是表单数据
    else:
        email = request.form.get('email')
    
    # 如果仍然有编码问题,可以尝试解码
    if email and '%40' in email:
        import urllib.parse
        email = urllib.parse.unquote(email)
    
    print(f"收到的邮箱: {email}")
    return jsonify({"status": "success", "email": email})

if __name__ == '__main__':
    app.run(debug=True)
  1. 快速测试:使用requests库测试你的端点
import requests
import json

# 测试JSON提交
response = requests.post(
    'http://localhost:5000/submit-email',
    json={'email': 'test@example.com'},
    headers={'Content-Type': 'application/json'}
)
print(response.json())

关键点: 前后端要保持一致的编码方式,推荐使用JSON格式传输数据。

总结:统一前后端的数据格式为JSON就能解决。

base64? flask 不改的有点麻烦。因为不转义特殊字符会被丢弃。

后端可以直接拿到数据,但是这哥们想不转义 http post

contentType: “application/json”,


?????????????

不是 啊,接受的是%40

我看有了加了这个后端可以接收到正常到 @,但是没有

你的 Content-Type 不是 json,是 application/x-www-form-urlencoded。MIME 类型标记为这个,后端才会主动 decode escape。

楼上正解。

回到顶部