jquery - Django 抛出错误编号。从 jQuery 接收 POST 时为 10053

标签 jquery ajax django http

我正在尝试使用 jQuery 向 Django 发送 POST 和 GET 请求。

到目前为止,只有 GET 被识别,而 POST 我会收到错误,尽管 GET 有效,但我无法获取发送到我的 Django 应用程序的各个字段。

所以我想知道问题出在哪里,以及如何让 POST 工作?

views.py

def xhr_test(request):
    if request.is_ajax():
        if request.method == 'GET':
            message = "Hello GET Ajax"
            print request.GET
            print "hello"
        elif request.method == 'POST':
            message = "Hello POST Ajax"
            print request.POST
    else:
        message = "Hello"
    return HttpResponse(message)

模板

<html>  
<head>  
  <title>Ajax with jQuery Example</title>  
  <script type="text/JavaScript" src="{{ MEDIA_URL }}js/jquery.js"></script>  
  <script type="text/JavaScript">  
  $(document).ready(function() {
    $("#generate").click(function() {
      $.get("/ajax_fetch/xhr_test", function(data) {
      // alert(data);
      });
    });

    $("#generate_post").click(function() {
      $.post("/ajax_fetch/xhr_test", {
        name: "Monty"
      }, function(data) {
          alert(data)
      });
    });
  });
  </script>  
<style type="text/css">  
    #wrapper {  
      width: 240px;  
      height: 80px;  
      margin: auto;  
      padding: 10px;  
      margin-top: 10px;  
      border: 1px solid black;  
      text-align: center;  
    }  
  </style>  
</head>  
<body>  
  <div id="wrapper">  
    <div id="quote"><p> </p></div> 
    <form method="get">
      <p><input type="text" name="user" /></p>
      <p><input type="submit" id="generate" value="Generate!" /></p>
    </form>

    <form method="post">{% csrf_token %}
      <p><input type="text" name="user" /></p>
      <p><input id="generate_post" type="submit" value="Click" /></p>
    </form>
  </div
</body>  
</html> 

错误跟踪

Exception happened during processing of request from ('127.0.0.1', 1625)
Traceback (most recent call last):

  File "c:\python27\lib\SocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "c:\python27\lib\SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "c:\python27\lib\SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python27\lib\site-packages\django\core\servers\basehttp.py", line 56
, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "c:\python27\lib\SocketServer.py", line 641, in __init__
    self.finish()
  File "c:\python27\lib\SocketServer.py", line 694, in finish
    self.wfile.flush()
  File "c:\python27\lib\socket.py", line 301, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in y
ur host machine

最佳答案

改变

<input id="generate_post" type="submit" value="Click" />

<input id="generate_post" type="button" value="Click" />

(将提交更改为按钮)

关于jquery - Django 抛出错误编号。从 jQuery 接收 POST 时为 10053,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851416/

相关文章:

jquery - 单击 css 类后获取最近的 iframe 的 src

javascript - 单选按钮更改事件并改进类似功能

javascript - 滚动经过特定 div 后隐藏 div

c# - 如何将 Signalr 扩展到大量用户

Javascript 对象原型(prototype)超出范围

django - 如何防止用户在未登录的情况下访问网页

javascript - 循环遍历数组,然后在最后一次出现后执行某些操作

javascript - JQuery 在 AJAX 查询后重新绑定(bind) vs 绑定(bind)

python - 如何在CSS文件中包含背景图片以供使用Django?

python - Django(1.11) 休息框架在嵌套序列化器中发布 user.id