如何获取“位置”带有 jQ​​uery 的 $.post 函数的标头?

发布于 2025-01-01 00:25:33 字数 1267 浏览 0 评论 0原文

我正在尝试创建一个可以处理表单的弹出框(使用 jQuery),并且在大多数情况下,我都可以正常工作。我遇到的唯一问题是,在成功登录和/或注册后,PHP 会发送一个标头位置以将用户重定向到他们的“主页”,并且它会在我加载表单的 div 中创建无限的重定向循环。

如果 post 请求中发送了 location 标头,我将如何读取,然后获取作为新 location 发送的实际 URL?

这是我到目前为止所尝试过的(除其他外):

$('.register').live('submit', function(e) {

    e.preventDefault();

    var form_data = $(this).serialize() + '&register=register&js=true';

    $.post(site_path + '/register', form_data, function(data, text, xhr) {

        alert(xhr.getResponseHeader("Location"));

        // $('.overlay_container').html(data);

    });

});

不确定我到底做错了什么,但据我所知,沿着这些思路的东西应该有效。我还尝试使用普通的 AJAX post 请求来执行此操作,但没有得到预期的结果。

请注意,不会总是发送位置标头;仅当有人成功登录、注册等时。

理想情况下,这就是我想要实现的目标:

$('.register').live('submit', function(e) {

    e.preventDefault();

    var form_data = $(this).serialize() + '&register=register&js=true';

    $.post(site_path + '/register', form_data, function(data, text, xhr) {

        var header_location = xhr.getResponseHeader("Location");

        if(header_location == null) {

            $('.overlay_container').html(data);

        }else{

            window.location = header_location;

        }

    });

});

I'm trying to create a pop up box (with jQuery) that can handle forms, and for the most part, I have it all working. The only issue that I have is that on a successful login and/or register, PHP sends a header location to redirect to the user to their "home", and it creates an endless loop of redirects with in the div that I loaded the form.

How would I go about reading if there was a location header sent on the post request, and then to get the actual URL that is being sent as the new location?

This is what I have tried so far (among other things):

$('.register').live('submit', function(e) {

    e.preventDefault();

    var form_data = $(this).serialize() + '®ister=register&js=true';

    $.post(site_path + '/register', form_data, function(data, text, xhr) {

        alert(xhr.getResponseHeader("Location"));

        // $('.overlay_container').html(data);

    });

});

Not sure what exactly I'm doing wrong, but as far as I know, something along those lines should work. I also tried doing this with a plain AJAX post request to no expected result.

Just to note, there wont always be a location header sent; only when someone successfully logs in, registers, etc.

Ideally, this is what I am trying to accomplish:

$('.register').live('submit', function(e) {

    e.preventDefault();

    var form_data = $(this).serialize() + '®ister=register&js=true';

    $.post(site_path + '/register', form_data, function(data, text, xhr) {

        var header_location = xhr.getResponseHeader("Location");

        if(header_location == null) {

            $('.overlay_container').html(data);

        }else{

            window.location = header_location;

        }

    });

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2025-01-08 00:25:33

据我所知,您实际上无法读取 Location 标头,因为 JavaScript 会跟随它,直到它们停止,然后将 headers 发回,这意味着我得到了标头目标页面,而不是最初发布到的页面。

因此,我编辑了 PHP 脚本中的重定向函数,以便在使用 JavaScript(或 jQuery)请求页面时发送自定义标头。

public function redirect($redirect = NULL) {

    global $_CONFIG;

    if($_POST['js'] == 'true') {

        exit(header('Redirect: ' . $_CONFIG['site']['path'] . '/' . $redirect));

    }else{

        exit(header('Location: ' . $_CONFIG['site']['path'] . '/' . $redirect));

    }

}

这样我就可以读取重定向是否实际发送,并在 JavaScript (jQuery) 端正确处理它。

最终结果如下:

$('.popup').on('submit', '.register', function(e) {

    e.preventDefault();

    var action = $(this).attr('action'), form_data = $(this).serialize() + '®ister=register&js=true';

    $.post(action, form_data, function(data, text, xhr) {

        var header_location = xhr.getResponseHeader('Redirect');

        if(header_location == null) {

            $('.overlay_container').html(data);

        }else{

            window.location = header_location;

        }

    });

});

As far a I am aware, you can not actually read a Location header as JavaScript follows it until they stop, and then sends the headers back, meaning that I get the headers of the destination page, and not the page that was initially posted to.

Therefore, I have edited my redirect function that I have in my PHP script to send a custom header if the page was requested using JavaScript (or jQuery).

public function redirect($redirect = NULL) {

    global $_CONFIG;

    if($_POST['js'] == 'true') {

        exit(header('Redirect: ' . $_CONFIG['site']['path'] . '/' . $redirect));

    }else{

        exit(header('Location: ' . $_CONFIG['site']['path'] . '/' . $redirect));

    }

}

That way I can read if the a redirect was actually sent, and handle it properly on the JavaScript (jQuery) end of things.

Here's what the final result looks like:

$('.popup').on('submit', '.register', function(e) {

    e.preventDefault();

    var action = $(this).attr('action'), form_data = $(this).serialize() + '®ister=register&js=true';

    $.post(action, form_data, function(data, text, xhr) {

        var header_location = xhr.getResponseHeader('Redirect');

        if(header_location == null) {

            $('.overlay_container').html(data);

        }else{

            window.location = header_location;

        }

    });

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文