如何获取“位置”带有 jQuery 的 $.post 函数的标头?
我正在尝试创建一个可以处理表单的弹出框(使用 jQuery),并且在大多数情况下,我都可以正常工作。我遇到的唯一问题是,在成功登录和/或注册后,PHP 会发送一个标头位置以将用户重定向到他们的“主页”,并且它会在我加载表单的 div 中创建无限的重定向循环。
如果 post 请求中发送了 location
标头,我将如何读取,然后获取作为新 location
发送的实际 URL?
这是我到目前为止所尝试过的(除其他外):
$('.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);
});
});
不确定我到底做错了什么,但据我所知,沿着这些思路的东西应该有效。我还尝试使用普通的 AJAX post 请求来执行此操作,但没有得到预期的结果。
请注意,不会总是发送位置标头;仅当有人成功登录、注册等时。
理想情况下,这就是我想要实现的目标:
$('.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;
}
});
});
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您实际上无法读取
Location
标头,因为 JavaScript 会跟随它,直到它们停止,然后将headers
发回,这意味着我得到了标头目标页面,而不是最初发布到的页面。因此,我编辑了 PHP 脚本中的重定向函数,以便在使用 JavaScript(或 jQuery)请求页面时发送自定义标头。
这样我就可以读取重定向是否实际发送,并在 JavaScript (jQuery) 端正确处理它。
最终结果如下:
As far a I am aware, you can not actually read a
Location
header as JavaScript follows it until they stop, and then sends theheaders
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).
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: