You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Header()重定向在网络活动中显示但页面未实际跳转求助

问题分析与解决方案

核心问题原因

你遇到的重定向不生效是因为AJAX请求的响应头不会触发浏览器页面跳转:当你通过jQuery.ajax请求proxy.php时,服务器返回的Location头只会被AJAX请求本身处理(浏览器会悄悄完成重定向请求,但不会更新当前页面),而非触发整个页面的跳转。另外你的proxy.php还存在一个错误:curl_error($ch)在curl_close($ch)之后调用,此时curl资源已释放,无法获取错误信息。

修复步骤

1. 修正proxy.php的curl错误检查逻辑

把错误检查移到curl_close之前,否则无法捕获curl请求的错误:

function makePostRequest($baseURL) {
    global $redirect_url;
    $ch = curl_init();
    $data = http_build_query($_POST);
    curl_setopt($ch, CURLOPT_URL, $baseURL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    
    // 先检查错误,再关闭curl资源
    if($e = curl_error($ch)) {
        echo json_encode(['status' => 'error', 'message' => $e]);
    } else {
        $json = json_decode($response, true);
        createCookie($json['authToken']);
        // 不直接跳转,返回重定向指令给前端
        echo json_encode(['status' => 'success', 'redirect' => $redirect_url]);
    }
    
    curl_close($ch);
    exit();
}

2. 修改AJAX回调,手动触发页面跳转

在AJAX的success回调中,根据服务器返回的指令执行页面跳转:

const submitButton = document.getElementsByClassName('login')[0];
submitButton.addEventListener('click', function(event) {
    event.preventDefault();
    const inputs = document.getElementsByTagName('input')
    const emailAddress = inputs[0].value
    const password = inputs[1].value        
    manageState().validateState();
    data = {
        name:'***',
        password: '***',
        userID: **,
        userSecret: ***
    }
    console.log('data', data)
    jQuery.ajax({
        type: "POST",
        url: "proxy.php?url=https://***/****",
        dataType: "json",
        contentType: 'application/x-www-form-urlencoded',
        data: data,
        success: function (res){
            console.log('success', res)
            // 手动跳转页面
            if(res.status === 'success') {
                window.location.href = res.redirect;
            }
        },
        error: function(xhr) {
            // 捕获请求错误,方便调试
            console.error('请求失败:', xhr.responseText);
        }
    });
});

备选方案:改用表单提交(无需AJAX)

如果不需要AJAX的异步体验,直接用HTML表单提交更简单,服务器端的header("Location: ...")会直接触发页面跳转:

<form method="POST" action="proxy.php?url=https://***/****">
    <input type="text" name="name">
    <input type="password" name="password">
    <!-- 其他隐藏字段:userID、userSecret -->
    <input type="hidden" name="userID" value="**">
    <input type="hidden" name="userSecret" value="***">
    <button type="submit" class="login">登录</button>
</form>

此时proxy.php可以保留原来的header("Location: $redirect_url")逻辑,无需返回JSON。

额外注意点

  • 确保$redirect_url变量在proxy.php中已正确定义(比如$redirect_url = '/';指向首页)
  • Cookie的过期时间设置为20秒(time() + 20)是测试用的,正式环境建议改回注释里的86400*30(30天)

内容的提问来源于stack exchange,提问作者Emm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:48:13