在 JQuery Ajax 调用后,无法从后续的 Slim URL 重定向请求标头中删除“X-Request-With”


After a JQuery Ajax call, unable to remove "X-Requested-With" from subsequent Slim URL redirect request headers

在JQuery AJAX调用之后,所有后续的Slim重定向都在请求标头中包含"X-Request-With:XMLHttpRequest"。因此,重定向页面的内容将在响应的后台返回,但浏览器不会重定向到预期的 URL。不知道我在这里做错了什么。

我的 Ajax 调用如下所示(这是以下实现: https://developers.google.com/+/web/signin/server-side-flow):

function signInCallback(authResult) {
    if (authResult['code']) {
        // Hide the sign-in button now 
        $('#signinButton').attr('style', 'display: none');
        // Send the code to the server
        $.ajax({
            type: 'GET',
            url: '../google/',
            contentType: 'application/octet-stream; charset=utf-8',
            success: function(result) {
                console.log(authResult['code']);
            },
            data: "gplustoken="+authResult['access_token'],
            error: function (request, status, error) {
                console.log("Error");
            }
         });
    } else if (authResult['error']) {
         // There was an error.
    }
}

}

PHP Slim URL 重定向代码如下:

$app->redirect($app->urlFor("home"));

有关上述行的上下文,请参阅:https://github.com/TheRosettaFoundation/SOLAS-Match/blob/master/ui/RouteHandlers/UserRouteHandler.class.php#L413

我试图从 PHP/Slim 代码的请求标头中删除"X-Request-With",如下所示,但它也没有奏效。

 $isAjaxRequest = $app->request->headers->get('X-Requested-With') == 'XMLHttpRequest';
 if ($isAjaxRequest) {
     if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
     {
         unset($_SERVER['HTTP_X_REQUESTED_WITH']);
         //$app->request->headers->set('X-Requested-With','');
         $app->redirect($app->urlFor("home"));
     }
  } else
       $app->redirect($app->urlFor("home"));

非常感谢任何解决此问题的帮助。

查看 Http Headers 对象的 Slim 文档,您应该能够通过调用 remove 方法来实现这一点:

$app->request->headers->remove('X-Requested-With');