gpt4 book ai didi

jquery - POST http ://127. 0.0.1:8000/notifications/ajax/403 (禁止)//使用ajax+django

转载 作者:行者123 更新时间:2023-11-28 19:14:13 32 4
gpt4 key购买 nike

为什么我会收到此错误,我想我有正确的 ajax 函数,我设置了正确的 url...请提供任何帮助,我们将不胜感激。

<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>

views.py

@login_required
def get_notifications_ajax(request):
notification = Notification.objects.get(id=id)
notes =[]

for note in notifications:
notes.append(str(note))
data={
"notifications":notes
}
json_data = json.dumps(data)
return HttpResponse(json_data, content_type='application/json')

并且在 urls.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'),

)

我已经尝试在我的 settings.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

最佳答案

您的数据正在使用我猜您尚未设置的 csrf_token 上下文变量,您更有可能想为 token 使用 django 模板标签

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

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

关于jquery - POST http ://127. 0.0.1:8000/notifications/ajax/403 (禁止)//使用ajax+django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619940/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com