岗位http://127.0.0.1:8000/notifications/ajax/403(禁止)//使用ajax+djang

2024-09-28 04:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我很感激我的网址设置错误,我会很感激的。在

<div id='notificationsLoader'>
</div>
<script>
$(document).ready(function(){
  $(".notification-toggle").click(function(e){
    e.preventDefault();
    $.ajax({
      type:"POST",
      url:"{% url 'get_notifications_ajax' %}",
      data: {
        csrfmiddlewaretoken: "{{csrf_token}}",
      },
      success: function(data){
        $("#notificationsLoader").html('<h3>notifications</h3>');
        $(data.notifications).each(function(){
          $("notificationsLoader").append(this + "<br/>")
        })
        console.log(data.notifications);
      },
      error: function(rs, e){
        console.log(rs);
        console.log(e);
      }


    })
  })
})
</script>

视图.py

^{pr2}$

网址.py

urlpatterns += patterns('notifications.views',
    url(r'^notifications/$', 'all', name='notifications_all'),
    url(r'^notifications/ajax/$', 'get_notifications_ajax', name='get_notifications_ajax'),
    url(r'^notifications/(?P<id>\d+)/$', 'read', name='notifications_read'),

)

我试过在我的设置.py但这行不通。我几乎可以肯定代码是正确的,因为我遵循的是一个教程,有了这段代码,讲师可以毫无问题地完成它。谢谢你

完全错误

POST http://127.0.0.1:8000/notifications/ajax/ 403 (FORBIDDEN)> ERROR)send @ jquery.min.js:4m.extend.ajax @ jquery.min.js:4(anonymous function) @ (index):359m.event.dispatch @ jquery.min.js:3r.handle @ jquery.min.js:3


Tags: namepylogurldatagetjsajax
1条回答
网友
1楼 · 发布于 2024-09-28 04:24:26

您的数据使用的是一个csrf_token上下文变量,我猜您还没有设置,您更可能希望使用django模板标记作为令牌

  url:"{% url 'get_notifications_ajax' %}",
  data: {
    csrfmiddlewaretoken: "{% csrf_token %}",
  },

除此之外,500错误是服务器错误,在调试和开发时,您应该将DEBUG设置设置为true来获取更多信息错误。在

相关问题 更多 >

    热门问题