我正在运行本地 django 服务器并执行简单的 ajax post 请求,但我不断收到此错误。我已经在 SO 上看到了与此相关的问题,这些问题指向 APPEND_SLASH 但这不是我的问题。 我有一个 UserProfile,我试图通过 ajax 查询保存它。服务器实际上执行ajax View ,我可以从 View 中看到打印,但它永远不会到达jquery ajax成功函数。
我可以从主页完美运行ajax View ,但不能从这个页面(即/edit_profile_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>
我的 ajax.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")
urls.py 看起来像这样。
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
----------------------------------------
*[更新]:*我也尝试将 urls.py ajax url 更改为此。
url(r'^edit_profile_details/ajax/save_profile/', save_profile, name='ajax_save_profile'),
这也没有解决问题。
最佳答案
也许这个答案有点晚了,但尝试修改你的js脚本来捕获事件,然后添加event.preventDefault();问题是,否则当 View 尝试返回响应时,客户端和服务器之间的连接将关闭。
关于django ajax 查询返回错误 10053 - 已建立的连接被主机中的软件中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142277/