【发布时间】:2013-12-22 15:07:55
【问题描述】:
几天前刚开始使用ajax,遇到了我需要的新情况。我关心的部分是加载。过去,我在 ajax 成功后加载了实际文件的内容....现在我想从我发布到的页面加载结果。
同样,在过去,我刚刚返回了一个数组 ['success']='success',也许还有一些其他变量用于返回调用并知道它是成功的。将大块 html 存储到单个变量中做同样的事情是最佳做法吗?
Say 'data'] 等于我要加载到 div 中的 html。这是这样做的最佳做法吗?我仍然需要检查处理的成功和失败,并且只有在成功时才从中加载数据。
$('#show-user').on('click', function () {
var $form = $(this).closest('form');
$.ajax({
type: 'POST',
url: '/spc_admin/process/p_delete_user_show.php',
data: $form.serialize(),
dataType : 'json'
}).done(function (response) {
if (response.success == 'success') {
$('#delete_user_info').fadeOut('slow', function(){
$('#delete_user_info').html('+response.data+', function() {
$('#delete_user_info').fadeIn('slow');
})
});
}
else
{
// error stuff
}
});
});
从 php 返回的示例:
$ajax_result['success'] = 'success'; // add success info
$ajax_result['data'] = 'this would be a large chunk of html'; // html to show
echo json_encode($ajax_result);
【问题讨论】:
-
也添加你的 php 代码
-
完成...只是看看这是否是最好的方法...在这样的单个变量中返回大量 html 似乎不正确。
-
将
return $ajax_result;更改为echo json_encode($ajax_result); -
哎呀...对不起,是的,我的代码中已经有了。上例中的输入错误。
-
不...我上面的方法在使用 load 而不是 html 时效果很好...当然在这些情况下我正在加载页面的内容而不是 ajax 结果。跨度>