Django开发服务器未处理Ajax请求 [英] Django development server not handling ajax request
问题描述
我正在执行一个简单的Ajax请求,但由于某种原因request.is_ajax返回false。我正在使用jquery和Django开发服务器。
I am making a simple ajax request but for some reason request.is_ajax return false. I am using jquery and Django development server.
$('#save').click(
function()
{
$.ajax({
type: "POST",
url: "/order/start",
});
});
在views.py
if request.POST and 'save' in request.POST :
if request.is_ajax()== True:
但是,它不会返回true,在运行服务器上,我会看到错误
But, it does not return true, and on runserver i see errors
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
推荐答案
我想您已经启用了标准中间件并设置了 settings.APPEND_SLASH
为True(默认设置),这意味着发布到 / order / start
会自动重定向到 / order / start /
带斜杠,在此过程中丢失POST。
I guess that you have the standard middleware enabled and settings.APPEND_SLASH
is True (the default), which means that POSTing to "/order/start"
automatically redirects to "/order/start/"
with a slash, losing the POST in the process.
请确保JS中的URL以斜杠结尾。
Make sure the URL in your JS ends with a slash.
这篇关于Django开发服务器未处理Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!