2016-07-31 104 views
-1

我的代码的相关部分如下所示。PHP标题重定向消息发送到浏览器,但不重定向

try 
{ 
    $customer = \Stripe\Customer::create(array(
    'email' => $_POST['custEmail'], 
    'source' => $_POST['stripeToken'], 
    'plan' => 'my_plan' 
)); 
header("Location:https://something.com"); 
    exit; 
} 
catch(Exception $e) 
{ 
    //header('Location:oops.html'); 
    echo "no work"; 
    error_log("unable to sign up customer:" . $_POST['custEmail']. 
    ", error:" . $e->getMessage()); 
} 

从我的PHP文件中的响应包括这样的:

HTTP/1.1 302 Found 
Date: Sun, 31 Jul 2016 21:13:52 GMT 
Server: Apache/2.4.23 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.22 
X-Powered-By: PHP/5.6.22 
Location: https://something.com 
Content-Length: 0 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: text/html; charset=UTF-8 

因此,该消息是回来(我猜的),但我的浏览器不重定向。我错过了什么?

+2

“消息回来(我猜)”是什么意思? – 2016-07-31 21:21:39

+0

@Dagon我的意思是基于我的PHP的响应头文件(显示问题),我的浏览器获取重定向消息,但不会对它做任何事情。 – jonmrich

+0

这样页面不会变成'https:// something.com'? – 2016-07-31 21:24:16

回答

3

对PHP的请求是Ajax ...响应数据是我尝试重定向到的页面的完整HTML代码。

位置标题意味着“您正在查找的资源已在此处”。这并不意味着“在主浏览器窗口中显示此URL处的资源”。浏览器视口不会重定向,无论处理请求会重定向。

浏览器将遵循重定向,并将响应传递给Ajax回调函数(除非触发错误,例如违反相同原点策略)。

如果要在浏览器中加载页面,请不要使用Ajax。提交常规表格。 Ajax的整点是避免在主视图中加载新文档。