gpt4 book ai didi

jquery - 我缺少什么? Django - Ajax

转载 作者:行者123 更新时间:2023-12-01 04:19:45 25 4
gpt4 key购买 nike

我正在遵循这个示例,但我想我错过了一些东西,因为它不起作用

#views.py
def message(request):
if request.is_ajax():
message = "I'm in Ajax"
else:
message = "I'm NOT in Ajax"
return HTTPResponse(message)

#html

<script type="text/javascript">
$(document).ready(function() {
$.get("/message", function(data){
alert(data);
});
});
</script>

#urls.py
#Display Message
(r'^message/$', message),

当我刷新页面时,什么也没有发生。当我在控制台中的 Google Chrome 中按 F12 时,出现错误:

GET http://127.0.0.1:8000/message/ 500 (INTERNAL SERVER ERROR) 

最佳答案

Django 没有 HTTPResponse 对象。您需要使用 HttpResponse 对象:

from django.http import HttpResponse

和:

return HttpResponse(message)

关于jquery - 我缺少什么? Django - Ajax ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924046/

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