Jquery.load() 函数在 Django 视图中被视为 ajax
我正在通过jquery.load(url)
加载内容
我想知道它是否被视为ajax调用,因为我在djnago中有循环
if request.is_ajax()
现在这个如果我使用 load 则不会执行循环,但如果我使用 $.ajax 或 post 或 get 则执行循环
I am loading the content via jquery.load(url)
I want to know is it considered as ajax call because i have loop in djnago
if request.is_ajax()
Now this loop is not executed if i use load but executed if i use $.ajax or post or get
is_ajax() 所做的只是检查请求中是否存在 HTTP_X_REQUESTED_WITH 标头。默认情况下,Jquery 会为所有包含 .load 的 .ajax 调用添加这一点。唯一不添加它的情况是请求被视为跨域。
但是,即使在这种情况下,如果您确实想覆盖它并设置 headers[ "X-Requested-With" ] = "XMLHttpRequest"; ,您仍然可以这样做
All is_ajax() does is checks for the existence of the HTTP_X_REQUESTED_WITH header in the request. By default Jquery does add that for all .ajax calls which would include .load. The only time that it does not add it is if the request is considered cross-domain.
However, even in that case you could still if you really wanted to override it and set headers[ "X-Requested-With" ] = "XMLHttpRequest";