使用ajax

时间:2017-08-18 18:19:49

标签: jquery ajax django forms semantic-ui

通过按ENTER提交表单时出现问题。

这就是我的js代码的样子:

  $.fn.api.settings.api = {
    'password_reset': '/account/password_reset/'
  }

  $('.ui.form')
    .form({
      fields: {
        email:  'empty'
      }
    })
    .api({
      action: 'password_reset',
      method: 'post',
      serializeForm: true,
      onSuccess: function () {
        $('.page.dimmer').dimmer('show');
        console.log('success')
      }
    })
  ;

如果我按下提交按钮,一切正常,但如果我按下ENTER提交表单,在控制台中我会得到:

[18/Aug/2017 21:05:20] "POST /account/password_reset/ HTTP/1.1" 200 6
Traceback (most recent call last):
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "c:\users\mihai\miniconda3\Lib\socket.py", line 593, in write
    return self._sock.send(b)
ConnectionAbortedError: [WinError 10053] An established connection has been dropped by the software on the host computer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Users\Mihai\Desktop\dev_place\env\lib\site-packages\django\core\servers\basehttp.py", lin
e 88, in handle_error
    super(ServerHandler, self).handle_error()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "c:\users\mihai\miniconda3\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:\users\mihai\miniconda3\Lib\socketserver.py", line 625, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\users\mihai\miniconda3\Lib\socketserver.py", line 354, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\users\mihai\miniconda3\Lib\socketserver.py", line 681, in __init__
    self.handle()
  File "C:\Users\Mihai\Desktop\dev_place\env\lib\site-packages\django\core\servers\basehttp.py", lin
e 155, in handle
    handler.run(self.server.get_app())
  File "c:\users\mihai\miniconda3\Lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "c:\users\mihai\miniconda3\Lib\wsgiref\simple_server.py", line 36, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

属性错误:' NoneType'对象没有属性' split'

为什么我只能按ENTER键才能收到此错误?

另外,我没有取得成功'控制台中的消息。

我用: Django 11.1, Jquery 3.2.1, Semantic-Ui 2.2, jQuery serializeObject 2.5.0(用于serializeForm)。 Windows 8.1

编辑:我将debug设置为true,然后我开始寻找一些错误: XHR Aborted (Most likely caused by page navigation or CORS Policy)

我在追踪调用中注意到ConnectionAbortedError: [WinError 10053] An established connection has been dropped by the software on the host computer

我还没想到它。

我该怎么办?

提前感谢您的回答!

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。

问题是当我按下ENTER时,不会调用.preventDefault()。

github上的一个人来了:

$("#form").keypress(function(event) {
        if (event.which == 13) {
            event.preventDefault();
        }
    });

现在它的工作正常,但哇,这个巨大的错误仅适用于preventDefault()。