jquery - Django - is_ajax() 不返回 true

标签 jquery python ajax django

我知道已经有很多人问过这个问题,但我无法解决它。请看一下:

从配置文件 View 中,当按下“更新”按钮时,我希望向用户显示一个可编辑的表单,其中包含从数据库中选取的值。所以目前还没有 POST 数据。

模板:

<form id="shw-form" action="/update_user_profile/{{r_user.id}}" method="get" enctype="multipart/form-data">
<button type="submit" id="upd-btn"  class="btn btn-lg btn-warning" onclick="update_profile_page();">UPDATE</button>
</form>

jquery:

<script type="text/javascript">
    function update_profile_page() {


            var form = $("#shw-form");
            $("#upd-btn").attr('disable', 'true');
            $.ajax({
                url: form.action,
                type: 'get',
                success: function(response){
                    $("#upd-main").load(response); 
                }
                });

            return false;

    }
   </script> 

url.py

url(r'^update_user_profile/(?P<user_id>\d+)$', 'myapp.views.update_user_profile', name='update_user_profile')

但是当我单击按钮时, is_ajax() 的 else 部分将被执行。

请看看您是否能找到我做错或遗漏的任何内容。谢谢。

最佳答案

is_ajax() 检查 X-REQUESTED-WITH header 。通常 jQuery 会正确设置它。您可以尝试在 $.ajax 中显式设置此 header :

$.ajax({
   url: form.action,
   type: 'get',
   success: function(response){
      $("#upd-main").load(response); 
   },
   headers: {
     'X-Requested-With': 'XMLHttpRequest'
   }
});

如果这不起作用,则意味着您的服务器或代理删除了此 header 。

关于jquery - Django - is_ajax() 不返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955607/

相关文章:

python - Pytorch 相当于 `tf.reverse_sequence` ?

java - 了解 Spring 3.0 示例中的 Ajax 简化

javascript - 在 asp.net 页面上制作覆盖/浅色背景 100% 高度

java - 当我尝试运行此应用程序时出现 setOnClickListener 错误

java - selenium webdriver无法找到隐藏在div中的元素

python - 可以通过命令行连接到数据库,但不能通过 Python 脚本连接到数据库?

带有复选框和 jQuery iCheck 的 jQuery 同位素组合过滤器

python mongodb $match 和 $group

jquery - jquery 对话框中的服务器端验证

jquery - 缩小 div 的内容而不是 DIV 本身。