简单封装一个异步 fecth,使用 async await 的方式来使用

(async () => {
    class HttpRequestUtil {
        async get(url) {
            const res = await fetch(url);
            const data = await res.json();
            return data;
        }
        async post(url, data) {
            const res = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            });
            const result = await res.json();
            return result;
        }
        async put(url, data) {
            const res = await fetch(url, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: JSON.stringify(data)
            });
            const result = await res.json();
            return result;
        }
        async delete(url, data) {
            const res = await fetch(url, {
                method: 'DELETE',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: JSON.stringify(data)
            });
            const result = await res.json();
            return result;
        }
    }
    const httpRequestUtil = new HttpRequestUtil();
    const res = await httpRequestUtil.get('http://golderbrother.cn/');
    console.log(res);
})();

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码中还有一个潜在的问题是 SQL 注入攻击。因为在 SQL 语句中使用了字符串格式化,而字符串格式化不能防止 SQL 注入攻击。应该使用参数化查询语句来避免这个问题。 修改后的代码如下: ``` def login_change(self): print(str(self.admin_username.get())) print(str(self.admin_pass.get())) admin_pass = None # 数据库操作 查询管理员表 db = pymysql.connect(host='localhost', port=3306, db='crime', user='qingying', password='123456') sql = "SELECT * FROM admin_id WHERE admin_id = %s" # 参数化查询语句 try: with db.cursor() as cursor: # 执行SQL语句 cursor.execute(sql, (self.admin_username.get(),)) # 获取所有记录列表 results = cursor.fetchall() for row in results: admin_id = row[0] admin_pass = row[1] # 打印结果 print("admin_id=%s,admin_pass=%s" % (admin_id, admin_pass)) except: print("Error: unable to fecth data") messagebox.showinfo('警告!', '用户名或密码不正确!') finally: db.close() # 关闭数据库连接 print("正在登陆管理员修改密码界面") print("self", self.admin_pass) print("local", admin_pass) if self.admin_pass.get() == admin_pass: AdminChange(self.window) else: messagebox.showinfo('警告!', '用户名或密码不正确!') ``` 修改后的代码使用参数化查询语句,将用户名作为参数传入 `execute()` 方法中,避免了 SQL 注入攻击。此外,使用了 `with` 语句来创建游标,避免了手动关闭游标的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值