Python中Django中的JS异步调用疑难杂症

Django中异步调用问题

对验证登录使用异步**

1,js异步请求代码

// HTML代码
<form method="post" id="login_form" onsubmit="return login()">
            {% csrf_token %}
            <p>
                <label for="uname">用户名:</label>
                <input type="text" id="uname" name="uname" placeholder="请输入用户名">
                <span class="error_tip" hidden="">提示信息</span>
            </p>
            <p>
                <label for="pwd">密码:</label>
                <input type="password" id="pwd" name="pwd" placeholder="请输入密码">
                <span class="error_tip" hidden="">提示信息</span>
            </p>
            <p>
                <input type="submit" id="submit" value="登录">
            </p>
        </form>
// 异步请求
function login() {
        $.post("{%url 'user:login_handle'%}",$("#login_form").serialize(),function (data) {
            if(data.status === "success"){  // 登录信息正确
                alert("成功!");
//                window.location.href = "{%url 'user:index'%}"
            }
            else{
                if(data.status === "name_error"){
                    $("#uname").val(data.uname);
                    $("#uname").next().html("用户名错误").show();
                }
                if(data.status === "pwd_error"){
                    $("#uname").val(data.uname);
                    $("#pwd").val(data.pwd);
                    $("#pwd").next().html("密码错误").show();
                }
            }
        });
    }

2,响应异步函数

def login_handle(request):
    try:
        uname = request.POST.get("uname","")
        pwd = request.POST.get("pwd","")
        username = Userinfo.objects.filter(username=uname)
        password = Userinfo.objects.filter(password=pwd)
        if len(username)==1 and len(password)==1:
            context = {"status":"success"}
            request.session["user_name"] = uname
            request.session["user_id"] = username[0].id
            return JsonResponse({"status":"success"})
        else:
            if len(username) !=1 :   # 用户名错误
                context = {"status":"name_error","uname":uname,"pwd":pwd}
                return JsonResponse(context)
            else:
                if len(password) !=1:    # 密码错误
                    context = {"status":"pwd_error","uname":uname,"pwd":pwd}
                    return JsonResponse(context)
    except Exception as e:
        print("====================>%s"%e)

**不知道为什么此处异步调用不能使用返回成功时JS的回调函数,像这样:不能够调用
$.post("{%url 'user:login_handle'%}",$("#login_form").serialize())
否则会报错。
如下:

` File “D:\Python3.6.1\lib\socketserver.py”, line 639, in process_request_thread

File “D:\Python3.6.1\lib\socketserver.py”, line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File “D:\Python3.6.1\lib\socketserver.py”, line 696, in init
self.handle()
File “D:\Python3.6.1\lib\site-packages\django\core\servers\basehttp.py”, line
155, in handle
handler.run(self.server.get_app())
File “D:\Python3.6.1\lib\wsgiref\handlers.py”, line 144, in run
self.close()
File “D:\Python3.6.1\lib\wsgiref\simple_server.py”, line 35, in close
self.status.split(’ ‘,1)[0], self.bytes_sent
AttributeError: ‘NoneType’ object has no attribute ‘split’`

不才,试过很多方法却没有成功,百度也无果。如果有哪位大兄弟以前遇到过这种,或者有什么意见或者方法的,麻烦留言告诉一声,先谢谢了。

猜你喜欢

转载自blog.csdn.net/weixin_39378885/article/details/79657473