我正在运行本地 django 服务器并执行简单的 ajax 发布请求,但我不断收到此错误。我已经在 SO 上看到了指向 APPEND_SLASH 的相关问题,但这不是我的问题。我有一个用户配置文件,我试图通过 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 更改为此。
url(r'^edit_profile_details/ajax/save_profile/', save_profile, name='ajax_save_profile'),
这也没有解决问题。