- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Ajax 与 Django 结合使用。数据被进一步传输,但不被识别为 Ajax。浏览器 Chrome/Firefox;
为了检查请求是否通过ajax完成,我使用:
`request.is_ajax()` method, but this fails, not true, so I checked this method how is working and:
def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
我验证了 META.get('HTTP_X_REQUESTED_WITH')
并得到 None
;
我认为是 ContentType 的问题,或者我需要设置另一个 header 。但如何?我的 JavaScript 是这样设置的:
xhr.open('post', event.target.action, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("HTTP_X_REQUESTED_WITH", "XMLHttpRequest");
xhr.send(data));
进一步检查似乎 request.Meta 也不包含 Content-type。
最佳答案
来自官方documentation :
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
所以 header 的名称应该是X-Requested-With
:
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
关于javascript - Django Ajax 通信,Django 不识别为 Ajax(使用 request.is_ajax() ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51466018/
我尝试用ajax构建一个小日志。我遵循了一个指南,但他在 php 中构建时没有类和函数。当我尝试通过将其放入类和函数中来重建整个应用程序时,我收到此错误: undefined index :is_aj
我有一个 Django 应用程序,它根据页面请求是否为 Ajax 返回不同的响应。 当我查看一个页面,导航到其他地方,然后单击后退按钮时,请求被呈现为好像 request.is_ajax() 的值为
我编写了一个 Django View ,它根据 request.is_ajax() 响应 text/html 或 application/json。到目前为止一切顺利,但是当我使用浏览器历史记录按钮时
HttpRequest.is_ajax()从 3.1 版开始不推荐使用。 如果页面是从浏览器请求的,我想返回 html 并且作为 JsonResponse如果从 javascript 或以编程方式调用
我正在尝试在模板中进行条件扩展: {% extends request.is_ajax|yesno:"base_ajax.html,base.html" %} 在我看来: def new(reques
我知道已经有很多人问过这个问题,但我无法解决它。请看一下: 从配置文件 View 中,当按下“更新”按钮时,我希望向用户显示一个可编辑的表单,其中包含从数据库中选取的值。所以目前还没有 POST 数据
我正在用 Django + jQuery 测试 ajax。我的看法是这样的: def ajax_test(request): if request.is_ajax(): mes
问题:如何重定向到 Django 中的页面,但将其“伪造”为 ajax 请求? 所以我有一个发布数据请求 - 通过 ajax (jQuery) 完成。这被发送到我的 django 服务器,它添加数据(
Kohana 如何确定请求是否为 AJAX 请求? referrer 字符串有什么不同吗?我是否需要添加 GET 参数,也许 ?ajax=true ? 最佳答案 它会检查请求是否由 XMLHttpRe
这个问题已经有答案了: AttributeError: 'WSGIRequest' object has no attribute 'is_ajax' (3 个回答) 已关闭去年。 我用 django
我正在学习在 Django 中使用 Ajax,许多教程只是检查 request.method == 'GET' 或 POST。我很好奇我们需要什么 .is_ajax() 。不使用它是正常现象还是教程只
我正在发送跨域 ajax 请求,而在另一端 request.is_ajax() 给出了 False var text = getSelectedText(); text = 'text'; i
上下文: Chrome 浏览器扩展使用 JQuery 请求远程 django 应用程序的响应。 Django 识别出该请求是通过 AJAX 发出的,并以“Hello AJAX!”进行响应。我的锻炼基于
我发现 Django 的 request.is_ajax 是一种非常有用的方法,可以通过 JS 添加渐进式增强,并且在我的观点中仍然保持 DRY。 但是,如果 request.is_ajax,我想使用
我的 Django View 中有这段代码: if request.is_ajax(): if request.method == 'POST': data = json.lo
我正在将 Ajax 与 Django 结合使用。数据被进一步传输,但不被识别为 Ajax。浏览器 Chrome/Firefox; 为了检查请求是否通过ajax完成,我使用: `request.is_a
我是一名优秀的程序员,十分优秀!