我知道已经有很多人问过这个问题,但我无法解决它。请看一下:
从配置文件 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/