我正在运行本地django服务器并执行一个简单的ajax post请求,但我继续收到此错误。我在SO上看到了相关的问题,指向APPEND_SLASH,但这不是我的问题。 我有一个UserProfile,我试图通过ajax查询保存。服务器实际执行ajax视图,我可以从视图中看到打印但它永远不会到达jquery ajax成功函数。
我可以从主页完美地运行ajax视图,但不能从 / 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(r'^edit_profile_details/ajax/save_profile/', save_profile, name='ajax_save_profile'),
这也没有解决问题。
答案 0 :(得分:0)
也许这个答案有点晚了但是尝试修改你的js脚本来捕获事件然后添加event.preventDefault();问题是,当视图尝试返回响应时,客户端和服务器之间的连接将被关闭。