我正在使用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)
}
});
}
我的Django视图:
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中给出(我想在这里做整页刷新)。
答案 0 :(得分:1)
你可以回答json编码,你将返回两个参数:
{
success: true/false,
data : YOUR_DATA_HERE
}
成功回调可以查看成功部分,并决定是否显示带有数据的弹出窗口,或者是否重新加载页面
抱歉,我不知道python,所以无法建议精确的表达式来进行正确的json编码。