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.
我正在尝试创建一个可以处理表单的弹出框(使用jQuery),而且在大多数情况下,我都可以使用它。我唯一的问题是,在成功登录和/或注册时,PHP发送一个标题位置以重定向到用户的“home”,并在div中创建一个无限循环的重定向,我加载了表单。
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
?
如果在帖子请求上发送了位置标题,然后获取作为新位置发送的实际URL,我将如何阅读?
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.
不确定我到底做错了什么,但据我所知,这些方面的东西应该有效。我也尝试使用普通的AJAX post请求执行此操作,但没有预期的结果。
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;
}
});
});
1 个解决方案
#1
1
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.
据我所知,你实际上无法读取一个Location标头,因为JavaScript会跟着它,直到它们停止,然后发回标头,这意味着我得到了目标页面的标题,而不是最初发布到的页面。
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).
因此,我已经编辑了我的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));
}
}
That way I can read if the a redirect was actually sent, and handle it properly on the JavaScript (jQuery) end of things.
这样我可以读取实际发送的重定向,并在JavaScript(jQuery)结束时正确处理它。
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;
}
});
});
#1
1
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.
据我所知,你实际上无法读取一个Location标头,因为JavaScript会跟着它,直到它们停止,然后发回标头,这意味着我得到了目标页面的标题,而不是最初发布到的页面。
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).
因此,我已经编辑了我的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));
}
}
That way I can read if the a redirect was actually sent, and handle it properly on the JavaScript (jQuery) end of things.
这样我可以读取实际发送的重定向,并在JavaScript(jQuery)结束时正确处理它。
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;
}
});
});