Jquery Django-AJAX POST导致管道破裂
我知道管道破裂是由于请求冲突造成的,所以帖子没有时间处理 我在这里发送以下两个请求:Jquery Django-AJAX POST导致管道破裂,jquery,python,ajax,django,Jquery,Python,Ajax,Django,我知道管道破裂是由于请求冲突造成的,所以帖子没有时间处理 我在这里发送以下两个请求: 阿贾克斯邮报 重定向到/message.html 因此event.preventDefault()可能会解决这个问题。但是,当我添加preventDefault()选项时,我根本没有收到POST请求。 添加超时将是一个非常糟糕的解决方案,因为它会在每次请求时停止 im获取的错误是: [13/Apr/2016 10:36:38]"POST /secreturl HTTP/1.1" 500 12202 - Brok
[13/Apr/2016 10:36:38]"POST /secreturl HTTP/1.1" 500 12202
- Broken pipe from ('127.0.0.1', 51133)
我的html页面:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href='https://fonts.googleapis.com/css?family=Merriweather+Sans' rel='stylesheet' type= 'text/css'>
<link href="{% static 'css/main.css' %}" type="text/css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" rel="stylesheet">
</head>
<script type = "text/javascript"
src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function clicked(){
//alert("Coming inside clicked!");
ValidateEmail();
}
function ValidateEmail(){
//ValidateEmail.preventDefault();
if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("validate-text").value)){
//alert("correct email!");
// do a post request to insert into db
var email = document.getElementById("validate-text").value
var datz = {
"email" : email,
csrfmiddlewaretoken:'{{csrf_token}}'
}
$.ajax(
{
url: '/secreturl',
type: "POST",
data: datz,
success: function (result) {
//alert('startline posted');
},
error: function (jqXhr, textStatus, errorThrown) {
//alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
}
});
return (true);
}else{
alert("You have entered an invalid email address!");
return (false);
}
}
</script>
<body>
<div class="site-wrapper">
<div class="site-wrapper-inner">
<div class="cover-container">
<div class="center-block col-md-7">
<div class="row">
<div class="col-md-12 padding0">
<img src="{% static 'img/CowsCrop.png' %}" width="90%">
<div class="image-caption">Her 'Achhe din' are definitely here. What about yours?</div>
<img src="{% static 'img/mobile.png' %}" width="70%" class="padding2">
<div class="form-group">
<div class="input-group col-md-10 col-md-offset-1">
<input type="text" class="form-control flat-form" name="validate-text" id="validate-text" placeholder="Leave your e-mail & be the 1st one to know when the awesomeness is out!" required>
<span class="input-group-addon danger flat-form button-color">
<!-- <a href="message.html" onclick="clicked()">I WANT IT!</a> -->
<a href="/message" onclick="return ValidateEmail();">Want It!</a>
</span>
</div>
</div>
</div>
<div class="pull-right padding1">
<img src="{% static 'img/facebook.png' %}" width="32px" height="32px">
<img src="{% static 'img/twitter.png' %}" width="32px" height="32px">
<img src="{% static 'img/instagram.png' %}" width="32px" height="32px">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-2.2.2.min.js" integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI=" crossorigin="anonymous"></script>
<script type="application/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</html>
将重定向代码放在success函数中
success: function (result) {
//alert('startline posted');
},
AJAX POST方法解决了我面临的类似问题。将重定向代码放在success函数中
success: function (result) {
//alert('startline posted');
},
< Ajax POST方法解决了我面临的一个类似问题。@ KoeBrad,我不认为服务器代码会帮助断开管道是当客户端中的一个请求被取消时。当浏览器导航到/消息时,它会中止ajax调用。您需要等待ajax调用完成后再执行重定向,或者将ajax参数直接传递到消息url,并在消息视图中处理它们(可以在前面添加另一个中间页,检查条件,然后重定向到消息)。@serg即使它导致错误,我的代码也可以正常工作。我能安全地忽略它还是会成为一个问题?@ KotruPipe,我不认为服务器代码会帮助断开管道是当一个客户端在中间取消请求时。当浏览器导航到/消息时,它会中止ajax调用。您需要等待ajax调用完成后再执行重定向,或者将ajax参数直接传递到消息url,并在消息视图中处理它们(可以在前面添加另一个中间页,检查条件,然后重定向到消息)。@serg即使它导致错误,我的代码也可以正常工作。我可以安全地忽略它,还是会成为一个问题?
success: function (result) {
//alert('startline posted');
},