django ajax查询返回错误10053-主机中的软件中止了已建立的连接
我正在运行一个本地django服务器并执行一个简单的ajaxpost请求,但我一直收到这个错误。我已经看到了关于这一点的相关问题,所以在这一点上附加_斜杠,但这不是我的问题。 我有一个UserProfile,我正试图通过ajax查询保存它。服务器实际上执行ajax视图,我可以从视图中看到打印结果,但它从未到达jqueryajax success函数 我可以在主页上完美地运行ajax视图,但不能在这个页面上运行,这个页面是/edit\u profile\u details页面。非常感谢你的帮助django ajax查询返回错误10053-主机中的软件中止了已建立的连接,django,jquery,Django,Jquery,我正在运行一个本地django服务器并执行一个简单的ajaxpost请求,但我一直收到这个错误。我已经看到了关于这一点的相关问题,所以在这一点上附加_斜杠,但这不是我的问题。 我有一个UserProfile,我正试图通过ajax查询保存它。服务器实际上执行ajax视图,我可以从视图中看到打印结果,但它从未到达jqueryajax success函数 我可以在主页上完美地运行ajax视图,但不能在这个页面上运行,这个页面是/edit\u profile\u details页面。非常感谢你的帮助
<script>
function js_save_profile(data) {
alert("This is a test");
$.ajax({
url: "{% url 'ajax_save_profile' %}",
type:"POST",
dataType: 'json',
success: function() {
added_html = "<div class='alert'><button type='button' class='close' data-dismiss='alert'>Hello</button><strong>Warning!</strong> Best check yo self, you're not looking too good. </div>"
$("#forms").append(added_html);
}
});
}
</script>
url.py如下所示
@csrf_exempt
def save_profile(request):
user_id=1
print
print
print "Save Profile Changes: "
user_profile = UserProfile.objects.get(id=user_id)
print user_profile.user.username
print "\n\n\n\n\n\n"
return HttpResponse(json.dumps({ 'user_name' : user_profile.user.username }), mimetype="application/javascript")
url(r'edit_profile_details/', edit_profile_details, name='edit_profile_details'),
url(r'^ajax/save_profile/', save_profile, name='ajax_save_profile'),
[16/Jun/2013 23:45:53] "GET /static/Highcharts-3.0.1/js/themes/gray.js HTTP/1.1"
304 0
Save Profile Changes:
sarangis
[16/Jun/2013 23:45:59] "POST /ajax/save_profile/ HTTP/1.1" 200 25
Traceback (most recent call last):
File "c:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "c:\Python27\lib\wsgiref\handlers.py", line 128, in finish_response
self.write(data)
File "c:\Python27\lib\wsgiref\handlers.py", line 212, in write
self.send_headers()
File "c:\Python27\lib\wsgiref\handlers.py", line 270, in send_headers
self.send_preamble()
File "c:\Python27\lib\wsgiref\handlers.py", line 194, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "c:\Python27\lib\socket.py", line 324, in write
self.flush()
File "c:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in yo
ur host machine
[16/Jun/2013 23:45:59] "POST /ajax/save_profile/ HTTP/1.1" 500 59
-[16/Jun/2013 23:45:59] "GET /edit_profile_details/? HTTP/1.1" 200 7229
---------------------------------------
Exception happened during processing of request from ('127.0.0.1', 29750)
Traceback (most recent call last):
File "c:\Python27\lib\SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "c:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 150
, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "c:\Python27\lib\SocketServer.py", line 651, in __init__
self.finish()
File "c:\Python27\lib\SocketServer.py", line 710, in finish
self.wfile.close()
File "c:\Python27\lib\socket.py", line 279, in close
self.flush()
File "c:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in yo
ur host machine
----------------------------------------
输出(包括错误)如下所示
*[更新]:*我还尝试将url.py ajax url更改为此
url(r'^edit_profile_details/ajax/save_profile/', save_profile, name='ajax_save_profile'),
这也没有解决问题。也许这个答案有点晚,但请尝试修改js脚本以捕获事件,然后添加event.preventDefault();问题在于,否则,当视图尝试返回响应时,客户端和服务器之间的连接将关闭