我正在使用 ajax 来执行该函数并检查该值是否已存在于数据库中。如果数据已经存在,我会显示 jQuery 对话框,该对话框工作正常,但如果该值尚不存在,我不想显示弹出窗口并刷新整个页面。这是我的 AJAX 函数:
function copyFile(val) {
var choices = document.getElementsByName("choice_shard_with_me").value;
var file_owner = document.getElementById('username').value;
$.ajax({
type: "GET",
url: "/copy_file/",
data: {choices:choices, file_owner:file_owner},
success: function(data){
$('#dialog-confirm').html(data)
}
});
}
我的姜戈视图:
if request.GET:
choices = request.GET.getlist('choice_shard_with_me')
file_owner = request.GET.getlist('username')
#Test if the file already exist in the user share directory
x = [File.objects.filter(user_id=request.user.id, file_name=i, flag='A').values_list('file_name') for i in choices]
y = [y[0] for y in x[0]]
if len(y) > 1:
return render_to_response('copyexist.html', {'file':y}, context_instance=RequestContext(request))
//doesn't refresh the whole page show the popup.
else:
//refresh whole page and do something
我的问题是:当显示弹出窗口时,它是在 div 中使用 Ajax 显示的。如果它进入 else 语句文件,则复制完成,并且成功的消息会在一个小 div 本身中给出(我想在这里刷新整个页面)。
您可以将答案设置为 json 编码,其中您将返回两个参数:
{
success: true/false,
data : YOUR_DATA_HERE
}
因此成功回调可以查看成功部分,并决定是否显示带有数据的弹出窗口,或使页面重新加载
抱歉,我不懂 python,所以无法建议准确的表达式来进行正确的 json 编码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)