问题描述
在我的 web 应用程序中,我打开了一个模态,在该模态中,我在另一个页面 (ajax.php) 中使用 ajax 处理一些数据,在 ajax.php 中完成处理后我会这样做:
In my webapp I have opened a modal and in that modal I'm processing some data using with ajax in another page(ajax.php), after the processing is done in the ajax.php I do:
header("location: blah.php");
但是由于我在另一个页面中并且整个过程都在处理 ajax 请求,所以页面没有做任何事情,当我通过 FireBug 检查 ajax 请求时,我看到请求变红并出现以下错误:
But since I'm in another page and the whole process is going on with ajax request the page doesn't do anything, when I check the ajax request via FireBug, I see the request goes red with this error:
302 Moved Temporarily error
在这种情况下如何重定向?
How can I redirect in this scenario?
提前致谢
推荐答案
响应已经重定向了 AJAX 请求.它不会更改用户当前在浏览器中看到的页面.就是这样,您无法从 PHP 中更改它.AJAX 请求完全在后台执行,这就是 AJAX 的意义所在.无论服务器返回什么响应,都不会直接明显地影响站点上当前对用户可见的任何内容.您必须完全用 Javascript 处理响应.
The response already redirects the AJAX request. It does not change the page the user is currently seeing in the browser. That's the way it is and you cannot change it from within PHP. The AJAX request was executed entirely in the background, that's the point of AJAX. Whatever response the server returns does not directly visibly affect anything on the site that's currently visible to the user. You have to handle the response entirely in Javascript.
如果您想根据失败的 AJAX 响应将用户重定向到另一个页面,您必须在 Javascript 中这样做:
If you want to redirect the user to another page based on a failed AJAX response, you'll have to do so in Javascript:
$.ajax(..., function (data) {
if (/* something or other */) {
window.location = /* other page */;
}
});
这篇关于PHP header(): 302 Moved 临时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!