2012-01-13 50 views
3

我试着将jQuery发送POST和GET请求到Django。Django抛出错误号。 10053当从jQuery接收POST时

到目前为止,只有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 

回答

0

好的,我不想调整问题。因此,请将我的解决方案作为答案发送给问题。下面是修改过的文件

模板

$(document).ready(function() { 
    $("#generate").click(function() { 
     $.get("/ajax_fetch/xhr_test",$("form:first").serialize(), function(data) { 
      alert(data); 
     }); 
     return false 
    }); 
    }); 

<form>{% csrf_token %} 
     <p><input type="text" name="user" /></p> 
     <p><input type="text" name="name" /></p> 
     <p><input type="submit" id="generate" value="Generate!" /></p> 
</form> 

views.py

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

有一个已经RY类似question

也许答案是settings.APPEND_SLASH设置,重定向到/ajax_fetch/xhr_test/ajax_fetch/xhr_test/。尝试发送数据到URL末尾用斜线

+0

,虽然这不是我的问题。无论如何,我解决了一些中伤后 – starcorn 2012-01-13 15:24:03

1

变化

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

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

(更改submitbutton

+0

非常感谢您的解决方案。但为什么'type =“submit”'导致这个错误?! – 2017-07-16 05:16:45