gpt4 book ai didi

javascript - 如何解决ajax请求后问题?

转载 作者:行者123 更新时间:2023-11-28 07:00:22 24 4
gpt4 key购买 nike

浏览次数:

@csrf_exempt
def client_data(request):
if request.method == 'POST':
form = formData(request.POST)
if form.is_valid():
ip = get_ip(request)
region = get_region(ip)
return redirect("/")

表格:

class formData(forms.Form):
email = forms.CharField(label='email', required=True, error_messages={'required': 'Укажите email'})
phn = forms.CharField(label='phone', required=True, error_messages={'required': 'Укажите phone'})
timezone = forms.CharField(label='timezone')

模板:

$(function () {
$('#send').click(function () {

var form = $('#send');
var send = {};
send['email'] = $("#email").val()
send['phn'] = $("#phn").val()
send['timezone'] = (new Date()).getTimezoneOffset().toString();

$.ajax({
url: "/sms/send/",
type: "POST",
data: send,//$("#send_form").serialize(), timezone,
dataType: "json",
success: function (response) {
alert("Данные отправлены");
},
error: function (xhr, textStatus, thrownError){
alert(thrownError)
}
});
return false;
})
});
<form class="form-inline text-left" id="send_form" method="post">
{% csrf_token %}
<div class="form-group">
<input type="email" name="email" value="" id="email" class="form-control field" placeholder="Ваш E-mail" />
</div>
<div class="form-group">
<input type="tel" name="phn" value="" id="phn" class="form-control field" placeholder="Ваш Телефон" />
</div>
<div class="form-group" style="margin-left: 30px">
<input id="send" type="submit" class="button send" value="Отправить">
</div>

服务器响应:/sms/send/处的值错误 View sms_send.views.client_data 未返回 HttpResponse 对象。它返回 None 相反。

我做错了什么?如何解决?

最佳答案

错误回溯非常清晰。每个 Django View 都必须返回一个 HttpResponse 对象。如果你想进行重定向,请这样做:

from django.http import HttpResponseRedirect

def client_data(request):
# do something ...
return HttpResponseRedirect('/')

关于javascript - 如何解决ajax请求后问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32191623/

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