是否可以从 jquery 加载的 php 文件重定向用户?
Is it possible to redirect user from jquery loaded php file?
我希望在通过 jquery
加载 php 文件时重定向用户
示例
$(form).submit(function(){
event.preventDefault();
$("#div").load("phpfile.php");
});
PHP
文件包含:header('Location: ...');
但是当 php 文件被加载时,当我在那里添加 header 时什么也没有出现...否则结果是好的并且被加载到指定的 div。有什么可能的方法可以将用户从加载的 php 文件重定向?谢谢。
如果您需要条件重定向,我建议您return json 回复:
各种响应看起来像:
{"redirect":false, "html":"success message html", "url": ""}
// OR
{"redirect":true, "html":"", "url": "/new/url"}
然后使用$.getJSON
代替load()
$.getJSON('phpfile.php', function(response){
if(response.redirect){
window.location = response.url;
}else{
$('#div').html(response.html);
}
}).fail(function(){ alert('Ooops we got a problem'); });
我希望在通过 jquery
示例
$(form).submit(function(){
event.preventDefault();
$("#div").load("phpfile.php");
});
PHP
文件包含:header('Location: ...');
但是当 php 文件被加载时,当我在那里添加 header 时什么也没有出现...否则结果是好的并且被加载到指定的 div。有什么可能的方法可以将用户从加载的 php 文件重定向?谢谢。
如果您需要条件重定向,我建议您return json 回复:
各种响应看起来像:
{"redirect":false, "html":"success message html", "url": ""}
// OR
{"redirect":true, "html":"", "url": "/new/url"}
然后使用$.getJSON
代替load()
$.getJSON('phpfile.php', function(response){
if(response.redirect){
window.location = response.url;
}else{
$('#div').html(response.html);
}
}).fail(function(){ alert('Ooops we got a problem'); });