此方法将允许您使用与$.post() 非常相似的流程,因为它对用户透明地发生并启用回调函数:
<img />
<iframe id="workFrame" style="display: none"></iframe>
<form action="php/navTabs.php" target="workFrame" method="post" style="display: none">
<input type=hidden id="hidden-1">
<input type=hidden id="hidden-2">
</form>
<script>
$('#hidden-1').val('some value to send to the server');
$('#hidden-2').val('some OTHER value to send to the server');
$('img').on('click', function () {
$('form').trigger('submit');
});
</script>
这使用了一个带有隐藏输入的表单(因此这对用户来说是透明的)。您可以使用 JavaScript 设置隐藏输入的值,然后以编程方式将表单提交到隐藏的 iframe。
此方法的另一个特点是您可以绑定到 iframe 的 load 事件,并像在 $.post() 中一样具有回调函数:
$('#workFrame').on('load', function () {
var response = $(this).contents().filter('body');
//if you output JSON in your PHP script you could parse this as JSON and do work
});
更新
如果您只想查看 PHP 脚本的输出,那么您可以使用开发人员工具(FireBug 等)查看响应。您还可以在 AJAX 回调中记录响应:
$.post("php/navTabs.php", { action: "deleteTab", theHTM: thehtm }, function(jdata) {
alert("The tab was " + jdata.is_deleted);
console.log(jdata);
}, "json");
如果您当前没有使用某些 Dev.带有控制台的工具,我强烈建议您检查FireBug,它将为您节省大量调试代码的时间。