I am trying to submit a form asynchronously using a form
tag, <input type='submit' method=get>
, and jQuery .
When I click the submit button I am getting the following error in my terminal traceback:
[24/Mar/2016 03:55:14] "GET /? HTTP/1.1" 200 1270 [24/Mar/2016 03:55:14] "GET /submitted/1458791714827 HTTP/1.1" 302 0 - Broken pipe from ('127.0.0.1', 33187)
Here is my HTML...
<body>
<h1>API: Disney</h1>
<form method="get">
<input type='submit' value='CLICK ME VIEW ALL TIMESTAMPS BETWEEN NOW AND 5 MINUTES AGO!'></input>
</form>
</body>
Here is my jQuery...
$(document).ready(function(){
$('form').submit(function(){
var submittime = new Date().getTime()
$.ajax({
url: 'submitted/' + submittime,
});
});
})
Here is my View...
class SubmitValue(View):
def get(self, request, currdate):
val = random.randrange(1,100)
date = int(currdate)
Data.objects.create(value=val, curr_time=date)
return redirect('/')
Here is my apps urls.py file...
url(r'^submitted/(?P<currdate>\d+)$', SubmitValue.as_view()),
Here is my Models...
from django.db import models
class Data(models.Model):
value = models.IntegerField()
curr_time = models.BigIntegerField()
When the input type=submit
is clicked, because I put method="get'
in the form
tag, it is sending an unnecessary get request that runs simultaneous to the $.ajax
get request. This causes the broken pipe
error. To get rid of it, I just pulled the method='get'
out of the form
tag so when the user clicks the input type=submit
it send only one get request.
Like this!
<body>
<h1>API: Disney</h1>
<form>
<input type='submit' value='CLICK ME VIEW ALL TIMESTAMPS BETWEEN NOW AND 5 MINUTES AGO!'></input>
</form>
</body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.