Php 函数在firebug中执行POST和GET请求,但没有头重定向
我有一个用jquery提交请求的表单Php 函数在firebug中执行POST和GET请求,但没有头重定向,php,html,post,redirect,header,Php,Html,Post,Redirect,Header,我有一个用jquery提交请求的表单 $("form input[type=submit]").live("click", function(event){ event.preventDefault(); //$(this).attr("disabled", "true"); var postdata = $(this).parents("form").serialize(); //console.log(data); $.ajax({
$("form input[type=submit]").live("click", function(event){
event.preventDefault();
//$(this).attr("disabled", "true");
var postdata = $(this).parents("form").serialize();
//console.log(data);
$.ajax({
type: "POST",
url: "../inc/process.inc.php",
data: postdata
});
});
这会导致用户在单击提交按钮时不会重定向到action=“inc/process.inc.php”method=“post”
process.in.php
$host = $_SERVER["HTTP_HOST"];
$actions = array(
'user_login' => array(
'object' => 'Intro',
'method' => 'processLoginForm',
'header' => 'Location: http://'.$host.'/homepage.php'
)
);
/*
* Make sure the anti-CSRF token was passed and that the
* requested action exists in the lookup array
*/
if ( isset($actions[$_POST['action']]) )
{
$use_array = $actions[$_POST['action']];
$obj = new $use_array['object']($dbo);
if ( true === $msg=$obj->$use_array['method']() )
{
header($use_array['header']);
exit;
}
else
{
// If an error occured, output it and end execution
die ( $msg );
}
}
else
{
// Redirect to the main index if the token/action is invalid
header("http://" . $host);
exit;
}
当我的方法返回true
时,这将导致用户重定向到
位置:http://.$host./homepage.php
但是相反,firebug给了我
POST
并返回firebug中的页面内容,然后
GET
如果最后一个else中的响应为空,请尝试使用
标题(“Location:http://”$host)代码>
关于您的问题,您需要执行一个ajax请求,并期望整个浏览器被重定向。阿贾克斯不是这样工作的。
如果执行ajax请求,则只有该请求将得到重定向,而不是父页面/窗口,甚至这也是可选的。
您应该返回一个脚本标记窗口。location='the new location'
,或者不通过ajax提交,并且这些内容以“老派”的方式流动,因此浏览器将拾取您发送的标题:)标题将无法按您期望的方式工作。header指令将重定向XML HTTP请求对象,而不是浏览器
编辑
你需要即兴发挥。按照以下思路做一些事情:
$.ajax({
type: "POST",
url: "../inc/process.inc.php",
data: postdata,
dataType: "json",
success: function (data) {
if (data.errorMessage) {
alert(data.errorMessage);
}
if (data.redirectTo) {
window.location = data.redirectTo;
}
}
});
在php代码中:
// replace the following line:
// header($use_array['header']);
// exit;
// with
echo json_encode(array(
"redirectTo" => $use_array["header"]
));
exit;
// replace the following line:
// die( $msg );
// with
echo json_encode(array(
"errorMessage" => $msg
));
exit;
// and so on