我正在用Django制作一个网站,它使用AJAX将一些信息发布到后端。本例中的信息是用户键入的总时间(从开始键入时起)到按submit时的总时间。但是,由于某些原因,我一直在命令提示符下看到错误。浏览器控制台只有0:,这表示POST中的错误。我在视图中有一组print语句,它们处理post(passage_result)来帮助定位错误,而且它似乎与return HttpResponse(json.dumps(response_data), content_type="text/html")行有关,因为每个print语句都返回预期的答案。在
错误如下:[04/Jan/2015 10:30:40] "POST /typer/passage_result/ HTTP/1.1" 200 140
Traceback (most recent call last):
File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Python34\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Python34\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Python34\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "C:\Python34\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "C:\Python34\lib\wsgiref\handlers.py", line 453, in _write
self.stdout.write(data)
File "C:\Python34\lib\socket.py", line 391, in write
return self._sock.send(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
[04/Jan/2015 10:30:40] "POST /typer/passage_result/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 58785)
Traceback (most recent call last):
File "C:\Python34\lib\wsgiref\handlers.py", line 138, in run
self.finish_response()
File "C:\Python34\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Python34\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Python34\lib\wsgiref\handlers.py", line 332, in send_headers
self.send_preamble()
File "C:\Python34\lib\wsgiref\handlers.py", line 255, in send_preamble
('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
File "C:\Python34\lib\wsgiref\handlers.py", line 453, in _write
self.stdout.write(data)
File "C:\Python34\lib\socket.py", line 391, in write
return self._sock.send(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\wsgiref\handlers.py", line 141, in run
self.handle_error()
File "C:\Python34\lib\wsgiref\handlers.py", line 368, in handle_error
self.finish_response()
File "C:\Python34\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data)
File "C:\Python34\lib\wsgiref\handlers.py", line 274, in write
self.send_headers()
File "C:\Python34\lib\wsgiref\handlers.py", line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File "C:\Python34\lib\wsgiref\handlers.py", line 344, in client_is_modern
return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\socketserver.py", line 613, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python34\lib\socketserver.py", line 344, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python34\lib\site-packages\django\core\servers\basehttp.py", line 129
, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "C:\Python34\lib\socketserver.py", line 669, in __init__
self.handle()
File "C:\Python34\lib\wsgiref\simple_server.py", line 133, in handle
handler.run(self.server.get_app())
File "C:\Python34\lib\wsgiref\handlers.py", line 144, in run
self.close()
File "C:\Python34\lib\wsgiref\simple_server.py", line 35, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
以下是passage_result视图:
^{pr2}$
这是我的jQuery代码://Create a time so we can update it later
var time = (new Date()).getTime();
//function to update the time when the user starts typing
$('#id_user_passage').on('keyup', function (event) {
event.preventDefault();
time = (new Date()).getTime();
});
// function to get the total time the user typed from when he started to when
// he clicked the submit button, and POST the information to the backend,
// along with what he typed and when he submitted it.
$('#passage_result_form').on('submit', function(event) {
event.preventDefault();
var total_time = (new Date()).getTime() - time;
var current_time = (new Date()).getTime();
console.log('Time passed : ' + total_time + ' milliseconds');
console.log('form submitted!')
$.ajax({
url: "/typer/passage_result/",
type: "POST",
data: { 'user_passage': $('#id_user_passage').val(), 'original_passage': $("#name").text(), 'time_taken': total_time,
'creation_time' : current_time},
success: function (json) {
total_time = 0;
console.log(json);
console.log("success");
},
error: function (xhr, errmsg, err) {
$('#results').html("
" ×
console.log(xhr.status + ": " + xhr.responseText);
}
});
location.replace('/typer/result/');
});