Django抛出错误没有。 10053从jQuery接收POST时 [英] Django throws error no. 10053 when receiving POST from jQuery
本文介绍了Django抛出错误没有。 10053从jQuery接收POST时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery向Django发送POST和GET请求。
I'm trying out jQuery to send POST and GET requests to Django.
到目前为止,只有GET被识别,而POST我将获得错误,尽管GET我无法获得发送到我的Django应用程序的各个字段。
So far only GET is recognized, and POST I will get errors, and although GET works I cannot get the individual fields that I send to my Django app.
所以我想知道问题是什么,以及如何让POST工作?
So I wonder what's the problem, and how do I get POST to work?
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
推荐答案
好的,我不想调整问题。因此,将我的问题解决方案作为答案发布。这是修改后的文件
Okay, I don't want to adjust the question. So post my solution to the problem as an answer instead. Here's the modified files
模板
$(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)
这篇关于Django抛出错误没有。 10053从jQuery接收POST时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文