Javascript 阿贾克斯邮报上的Django[WinError 10053]

Javascript 阿贾克斯邮报上的Django[WinError 10053],javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,我正在用Django制作一个网站,它使用AJAX将一些信息发布到后端。本例中的信息是用户键入的总时间(从开始键入到按submit)。但是,由于某些原因,我在命令提示符下不断出现下面的错误。浏览器控制台只有0:,这表示文章中有错误。我在视图中有一堆print语句处理post(passage\u result)来帮助定位错误,它似乎与行return HttpResponse(json.dumps(response\u data),content\u type=“text/html”)有关,因为每个p

我正在用Django制作一个网站,它使用AJAX将一些信息发布到后端。本例中的信息是用户键入的总时间(从开始键入到按submit)。但是,由于某些原因,我在命令提示符下不断出现下面的错误。浏览器控制台只有
0:
,这表示文章中有错误。我在视图中有一堆print语句处理post(
passage\u result
)来帮助定位错误,它似乎与行
return HttpResponse(json.dumps(response\u data),content\u 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'
----------------------------------------
以下是
段落\u结果
视图:

@csrf_exempt
def passage_result(request):
    if request.method == 'POST':
        # Get all of the information needed
        user_passage = request.POST.get('user_passage')
        total_time = request.POST.get('time_taken')
        current_passage = request.POST.get('original_passage')
        creation_date = request.POST.get('creation_time')
        user = request.user
        original_passage = Passage.objects.get(name=current_passage, user=user)

        print("THIS IS THE TOTAL TIME " + str(total_time))

        # Measure word length and character length so I can measure accuracy/wpm
        user_split = re.findall(r'((?:(?<=^\s)\s*)?\S+\s*(?=\s\S|$))', user_passage)
        user_wlength = len(user_split)
        user_clength = len(user_passage)

        # Compare both strings against one another
        errors, accuracy, error_indices = checkString.checkWord(original_passage.text_body, user_passage)

        print("Errors: "+ str(errors))
        print("Accuracy: " + str(accuracy))

        # Round up the numbers, calculate wpm and cpm
        accuracy = round(accuracy, 4)
        minutes = float(total_time) / float(60000)
        wpm = round(float(user_wlength/minutes), 2)
        cpm = round(float(user_clength/minutes), 2)

        overall = round((wpm + accuracy)/2, 2)

        # Create a passage result for this instance, and save it to the database

        passageResult = PassageResult(user=user, passage=original_passage, creation_date=creation_date, user_passage=user_passage, total_time=total_time,
                                  total_words=user_wlength, total_chars=user_clength, errors=errors, accuracy=accuracy,
                                  wpm=wpm, cpm=cpm, overall=overall)
        passageResult.save()

        # Get the ranking of each passage, and save it.
        sorted_results = PassageResult.objects.order_by('-overall')
        for i in range(0, len(sorted_results)-1):
            sorted_results[i].rank = i + 1
            sorted_results[i].save()
            print(sorted_results[i].rank)

        # Create the response data that will be dumped json style
        response_data = {}
        response_data['result'] = 'Passage submitted successfully!'
        response_data['user_text'] = passageResult.user_passage
        response_data['total_time'] = passageResult.total_time
        response_data['author'] = passageResult.user.username


        return HttpResponse(
            json.dumps(response_data),
            content_type="text/html"
        )
    else:
        return HttpResponse(
            json.dumps({"nothing to see": "this isn't happening"}),
            content_type="text/html"
        )
@csrf\u豁免
def通道_结果(请求):
如果request.method==“POST”:
#获取所需的所有信息
user\u passage=request.POST.get('user\u passage')
总时间=request.POST.get('time_taked')
当前文章=request.POST.get('原始文章')
创建日期=request.POST.get('创建时间')
user=request.user
原始通道=通道.objects.get(名称=当前通道,用户=用户)
打印(“这是总时间”+str(总时间))
#测量字长和字符长度,以便我可以测量精度/wpm

user_split=re.findall(r’(?:(?我遇到了同样的问题,并通过这样做解决了它,只需在ajax中添加“async:false”

//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("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: " + errmsg +
                 " <a href='#' class='close'>&times;</a></div>");
             console.log(xhr.status + ": " + xhr.responseText);
         }
     });
     location.replace('/typer/result/');

 });