js知识整理

ajax:

后台处理业务返回JsonResponse

    if request.method == 'GET':
        result=models.Project.objects.all()
        return render(request, 'project/project-list.html',{'result':result})
    if request.method=='POST':
        try:
            print(request.POST)
            pname=request.POST.get('projectname')
            pverson=request.POST.get('projectid')
            pdesc=request.POST.get('projectdesc','')
            user=request.session.get('user')
            u_obj=User.objects.filter(username=user).first()
            obj=models.Project.objects.filter(name=pname).first()
            if obj:
                return JsonResponse({'code':2001,'msg':'项目已存在'})
            else:
                models.Project.objects.create(name=pname,verson=pverson,desc=pdesc,user_id=u_obj.id)
                return JsonResponse({'code': 2000, 'msg': '项目创建成功'})
        except Exception:
            return JsonResponse({'code':2002,'msg':'项目创建失败'})

 

js接受返回状态码:

 $.ajax({
           url: '/project/project-list.html',
           type: 'POST',
           data: $('#fm').serialize(),
           success: function (result) {
                  let code = result.code;
                  if (code == '2000') {
                      show_notify("success", result.msg);
                      window.location.href = "/project/project-list.html";
                   } else if (code == '2001') {
                       show_notify("error", result.msg);
                   } else if (code == '2002') {
                       show_notify("error", result.msg);
                   }
               }
        })

 

<tr>
     <td>11</td>
     <td>22</td>
     <td><a href="#">33</a></td>
</tr>
<tr>
     <td>11</td>
     <td>22</td>
     <td><a href="#">33</a></td>
</tr>
<tr>
     <td>11</td>
     <td>22</td>
     <td><a href="#">33</a></td>
</tr>

移除点击所在的tr

$(this).parent().parent().remove();

 通过cookie获取csrf_token,每次ajax提交前自动带csrf:

$(function () {
        $.ajaxSetup({
            headers: {"X-CSRFToken": getCookie("csrftoken")}
        });

    });

    function getCookie(name) {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        console.log(document.cookie.match(reg));
        if (arr = document.cookie.match(reg)) {
            console.log(arr[2]);
            console.log(unescape(arr[2]));
            return unescape(arr[2]);
        }
        else{
             return null;
        }

    }
console.log(document.cookie.match(reg)):
(4) ["csrftoken=fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j", "", "fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j", "", index: 0, input: "csrftoken=fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j", groups: undefined]
0: "csrftoken=fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j"
1: ""
2: "fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j"
3: ""
groups: undefined
index: 0
input: "csrftoken=fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j"
length: 4
__proto__: Array(0)

 

console.log(arr[2]):
fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j
 
console.log(unescape(arr[2]));
fxmUfi9cuqzqcbPXNxXNyeztfqEWXJ8c04CiNqXInZ3bNVtSZmYt35JsSTn4ym9j

 

 bootstrap modal 加滚动条
.modal-dialog {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.modal-content {
    /*overflow-y: scroll; */
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
}

.modal-body {
    overflow-y: scroll;
    position: absolute;
    top: 55px;
    bottom: 65px;
    width: 100%;
}

.modal-header .close {margin-right: 15px;}

.modal-footer {
    position: absolute;
    width: 100%;
    bottom: 0;
}
View Code

 

 
posted @ 2018-09-27 13:27  oneforall97  阅读(22)  评论(0编辑  收藏  举报