django中使用ajax传输数据出现Forbidden (CSRF token missing or incorrect.): /index/mark/

解决办法:from django.views.decorators.csrf import csrf_exempt

                    然后在对应的view前加上@csrf_exempt

eg:

        ajax部分

        $.ajax

                ({
                  url: "/index/mark/",
                  type:"POST",
                  data:{
                    'url':url,
                    'csrfmiddlewaretoken':$('[name="csrfmiddlewaretoken"]').val()
                    },
                  success:function(data){
                    var houseInfos = JSON.parse(data);
                    console.log(houseInfos);
                    }

                });

        view部分

        @csrf_exempt
        def mark_views(request):
            if request.is_ajax():
                # url = 'http://www.baidu.com'
                    URL = request.POST.get('url')
                    rep = bs(requests.get(URL).text,'lxml')
                    print(rep)

阅读更多

没有更多推荐了,返回首页