我正在尝试将数据发送到数据库而不必加载新页面。这是我的Javascript:
function submit(){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/185post/", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("noun=bananas&userAnswer=textajaxNow");
}
这将由views.py中的以下功能处理:
def _185post(request):
joke =Joke185.objects.all().filter(noun=request.POST.dict()['noun'])
_185Joke = Joke(text=request.POST.dict()['userAnswer'],source='improv')
_185Joke.save()
print (_185Joke)
joke[0].joke.add(_185Joke)
joke[0].answers += 1
print (joke[0].answers)
joke[0].save()
print (joke[0].save())
您可以看到那里有一些打印语句,它们都返回了我期望的打印语句,所以我不确定为什么会收到500 Internal Service Error
答案 0 :(得分:1)
如果您退货,您的问题将得到解决,但是还有更多代码建议
1-不要使用_来启动功能
2-使用记录器代替打印
from django.http import JsonResponse
def _185post(request):
joke =Joke185.objects.all().filter(noun=request.POST.dict()['noun'])
_185Joke = Joke(text=request.POST.dict()['userAnswer'],source='improv')
_185Joke.save()
print (_185Joke)
joke[0].joke.add(_185Joke)
joke[0].answers += 1
print (joke[0].answers)
joke[0].save()
print (joke[0].save())
return JsonResponse(joke[0])