大佬教程收集整理的这篇文章主要介绍了jquery – 为什么在AJAX请求中使用@csrf_exempt时会出现403 Forbidden错误?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
def profile_listview(request,username,template_name=userena_setTingS.USERENA_PROFILE_DETAIL_TEMPLATE,extra_context=None,**kwargs): user = get_object_or_404(get_user_R_511_11845@odel(),username__iexact=userName) fullSALElist = Entry.objects.filter(author__username__iexact=userName) @csrf_exempt def delete_object(request): if request.is_ajax(): print "request is ajax" object_name = request.POST.get('entryname') targetobject = Entry.objects.get(headline=object_name) if request.user.username == targetobject.author: targetobject.delete() print "Hello" return httpResponseRedirect('/storefront/')
和模板中的AJAX代码:
<script type="text/javascript"> var my_app = { username: "{{ request.user.username }}" }; </script> <script> $(document).ready(function() { $(".delete_button").click(function() { var id = $(this).attr('id'); $.ajax({ type: "POST",url: "/accounts/" + my_app.username + "/listview/",data: { entryname:id },}); return false; }); }); </script>
URLS
(r'^accounts/(?P<username>[\@\.\w-]+)/listview/$',profile_listview),
值得注意的事情:
>我在我的设置中打开了csrf中间件
>在jQuery AJAX代码中,url和数据都发送正确的信息
>当我单击删除按钮时,我收到403禁止错误.
>打印“请求是ajax”不会在控制台(或任何地方)打印.
我也很困惑,因为我收到了相互矛盾的信息.有人告诉我应该通过javascript(https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/)添加csrf值.这给我留下了2个问题. 1.这与在我的POST请求中添加csrfmiddlewaretoken:'{{Csrf_token}}’有什么不同? 2.更重要的是,使用@csrf_exempt时,我仍然得到403错误的事实使得这个问题没有实际意义吗?
以上是大佬教程为你收集整理的jquery – 为什么在AJAX请求中使用@csrf_exempt时会出现403 Forbidden错误?全部内容,希望文章能够帮你解决jquery – 为什么在AJAX请求中使用@csrf_exempt时会出现403 Forbidden错误?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。