简体   繁体   English

通过AJAX的header(Location :)不重定向

[英]header(Location:) via AJAX not redirecting

First off, let me say that I know this does not seem like an uncommon issue. 首先,我要说的是,我知道这似乎不是一个罕见的问题。 I do not intend to clutter these forums with questions that have already been answered, but, I have worked through probably about 3 dozen threads with similar issues and have been unable to reach a solution. 我不打算在这些论坛上堆满已经回答的问题,但是,我可能已经研究了大约3十几个存在类似问题的线程,但仍未找到解决方案。

The short description of my issue is this: I am trying to redirect after submitting a form using php's header(location:) function, and it's not redirecting. 我的问题的简短描述是这样的:我正在尝试使用php的header(location :)函数提交表单后进行重定向,并且未进行重定向。 It is a little bit complicated because the form is submitted via AJAX. 这有点复杂,因为该表单是通过AJAX提交的。 The steps my code goes through are as follows: 我的代码经过的步骤如下:

User submits form > form data sent via AJAX > form processing page validates data > if valid, form processing page inserts data into database > if submission is successful, form processing page adds redirect URL to $_SESSION > form processing page returns a 'redirect' variable > javascript/AJAX checks for redirect variable, and refreshes page if found > page header checks $_SESSION for redirect URL, and if found, sets appropriate headers and exits. 用户提交表单>通过AJAX发送的表单数据>表单处理页面验证数据>如果有效,表单处理页面会将数据插入数据库中>如果提交成功,表单处理页面会将重定向URL添加到$ _SESSION>表单处理页面返回“重定向” > javascript / AJAX变量检查重定向变量,并在找到页面时刷新页面>页面标题检查$ _SESSION以获得重定向URL,如果找到,则设置适当的标题并退出。

I guess the first thing I want to ask is, is this the right way of going about this? 我想首先要问的是,这是正确的解决方法吗? It seems to me that there should be a simpler way of doing this. 在我看来,应该有一种更简单的方法。 It's obviously much simpler to pass the redirect URL to the javascript and do a window.location redirect, but I read that it's much better/more secure to handle that on the server side, so I'm trying to do that. 将重定向URL传递给javascript并执行window.location重定向显然要简单得多,但是我读到在服务器端处理它要好得多/更安全,所以我正在尝试这样做。

Assuming I'm going about this in the right direction, what could be going wrong here? 假设我朝着正确的方向前进,那么这里可能出什么问题了? I've checked for leading and trailing whitespace, BOM characters, I've even tried output buffering and I still have the same issue. 我检查了前导和尾随空格,BOM字符,甚至尝试了输出缓冲,但仍然遇到相同的问题。

What happens on form submission is, the page refreshes, but it returns to the original form page rather than the redirect url. 提交表单时,页面会刷新,但它会返回到原始表单页面,而不是重定向URL。 I have turned on the most detailed error reporting and get no errors at all. 我已经打开了最详细的错误报告,并且完全没有错误。

One thing that may or may not be of interest, I have an error_log function set up to log all headers to a file right after I set the Location: header. 一件事可能不感兴趣,我设置了error_log函数,以便在设置Location:标头后立即将所有标头记录到文件中。 When I redirect outside of AJAX (which works), the accept: header is set to html, but when I try to do it via AJAX, the accept header is set to JSON. 当我重定向到AJAX之外(有效)时,accept:标头设置为html,但是当我尝试通过AJAX执行此操作时,accept标头设置为JSON。 Could that cause a problem? 会造成问题吗?

Thank you so much for taking the time, and again, apologies if this is a dumb question. 非常感谢您抽出宝贵的时间,如果这是一个愚蠢的问题,请再次道歉。 I have used this forum for years and have never once had to post a question on it because it has always solved my problems until now. 我使用这个论坛已有很多年了,从来没有关于它提出过任何问题,因为到目前为止,它一直可以解决我的问题。 Thanks in advance! 提前致谢!

PHP is too generous to include in your code not only HTML code, but also JavaScript code. PHP非常慷慨,无法在您的代码中不仅包含HTML代码,还包含JavaScript代码。 I'll explain one thing. 我会解释一件事。 When you send data by ajax, it is often difficult return Boolean data (whether true or false, depending on the conditions we work side server with php in some file established in our direction ajax) to give a final answer. 当您通过ajax发送数据时,通常很难返回布尔数据(无论是true还是false,这取决于在ajax方向建立的某些文件中php在端服务器上工作的条件)才能给出最终答案。

On the other hand, returning to the generosity of PHP, always when we try to validate data server-side and through ajax, we can manipulate the section where work freely to show some response message, a redirect to somewhere on your page or create a session. 另一方面,回到PHP的慷慨之处,总是在我们尝试通过ajax验证数据在服务器端并通过ajax进行操作时,我们可以自由操作工作部分以显示一些响应消息,重定向到页面上的某处或创建一个会议。 Anyway, whatever. 无论如何

What I mean is that in your controller section where you will work with AJAX, you can set different conditions. 我的意思是,在将要使用AJAX的控制器部分中,您可以设置不同的条件。 That is, if the user data are correct, then you could send to call a script from Javascript to redirect him with a location.reload (); 也就是说,如果用户数据正确,则可以发送从Javascript调用脚本以使用location.reload();重定向他。 and also assign a session automatically. 并自动分配会话。 If the user does not have the correct data, then what we should do is return a warning message or alert to the exit AJAX, usually it goes after a: 如果用户没有正确的数据,那么我们应该做的是向出口AJAX返回一条警告消息或警报,通常它会在a之后:

function (response) {
$ ('.answer').html(response);
}

Note that it is not necessary require, for example, a $ ('.answer').html(response); 请注意,没有必要要求例如$('.answer')。html(response); to return an answer, because ajax only sends data to the server and if there is a reply message well, and if not well. 之所以返回答案,是因为ajax仅将数据发送到服务器,并且是否存在答复消息,如果不是,则很好。 Many times what I do, is to send messages via console, although it is often convenient to do so, because as often work with several conditions and that depends on the result of our request, we will be forced to show some response in some element within our code. 我经常做的是通过控制台发送消息,尽管这样做通常很方便,因为由于经常在多个条件下工作,并且这取决于我们请求的结果,因此我们将不得不在某些元素上显示一些响应在我们的代码中。

What I advise you is that you work javascript in PHP, and hence redirect to other pages, assign sessions or simply return a message. 我建议您使用PHP使用JavaScript,从而重定向到其他页面,分配会话或仅返回消息。 Remember that an ajax request is not only asynchronous, but repetitive and can send it to call as often as you need. 请记住,一个ajax请求不仅是异步的,而且是重复的,并且可以根据需要将其发送给调用。 The fact that you sent your ajax call php file and you have returned an answer, does not mean you can go back to work with it once you finish your ajax request. 您发送了ajax调用php文件并返回了答案,这一事实并不意味着您一旦完成ajax请求就可以重新使用它。

It is the right way to do what you want, it is often easier to manipulate our server-side code that client side. 这是做您想要的事情的正确方法,通常比操作客户端的服务器端代码更容易。 Greetings. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM