【发布时间】:2015-10-24 06:14:30
【问题描述】:
我正在进行如下所述的 ajax 调用。我正在开发一个简单的退订表单,用户在其中输入电子邮件并从邮件警报中退订。 使用django框架
<html>
<head>
<script src="https://code.jquery.com/jquery.min.js" ></script>
</head>
<body>
<div id="errorDiv"></div>
<form id="unsubscribeform" class="unsubscribeform" method="POST" action="{% url 'polls:unsubscribe' %}" enctype="multipart/form-data">
{% csrf_token %}
<label class="required" for="emailUnsubscription">Email</label>
<input id="emailUnsubscription" name="unsubscribeEmail" type="email"><br><br>
<input value="unsubscribe" class="submit" id="submit" type="submit"/>
</form>
<script>
$(document).ready(function()
{
$('.submit').bind('click', function(event)
{
event.preventDefault();
var form_data = new FormData($(".unsubscribeform")[0]);
$.ajax({
type: 'POST',
url:"?ajax=1",
data:form_data,
processData: false,
contentType: false,
success:function(response)
{
var resp = response;
var noUserError = response.nouser;
var emptyEmailError = response.emptyemail;
if(typeof emptyEmailError === 'undefined' && typeof noUserError === 'undefined'){
$(.submit).unbind('click').submit();
}else{
if(typeof emptyEmailError === 'undefined')
emptyEmailError = "";
if(typeof noUserError === 'undefined')
noUserError = "";
$(".errorlist").remove();
console.log(noUserError, emptyEmailError);
$("errorDiv").append('<span class="errorlist"><br>'+Please correct following errors+'</span>');
$("errorDiv").append('<span class="errorlist"><br>'+noUserError+'</span>');
$("errorDiv").append('<span class="errorlist"><br>'+emptyEmailError+'</span>');
}
}
});
});
});
</script>
</body>
在 html 中成功返回我的表单后,我丢失了,我只得到一个带有 json 返回的普通 html。
ajax调用后浏览器显示如下文字
{"nouser": "No user exists with given email id"}
我做错了什么
【问题讨论】:
-
你能不能也展示一下php的一面?