javascript jQuery.post() 并通过 ajax 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9001666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.post() and redirect via ajax
提问by MrIzik
I am using jQuery.post()
to send data to the server, when the server sends data back to the client, the post() callback is invoked. I know that the server might response with the redirect header field ("Location
").
我正在使用jQuery.post()
向服务器发送数据,当服务器将数据发送回客户端时,会调用 post() 回调。我知道服务器可能会使用重定向标头字段 (" Location
") 进行响应。
currently, the redirect does not occur. what can be the reason? is there any possibility to run a script before the redirect occur?
目前,不会发生重定向。可能是什么原因?是否有可能在重定向发生之前运行脚本?
UPDATE:
enclosed a snipped code. i know that the POST method is accepted by the server, and that the server responds to the POST. somehow, always the error()
is being invoked (it seems like it happends even before the response is accepted by the client\browswer).
更新:附上一段代码。我知道服务器接受 POST 方法,并且服务器响应 POST。不知何故,总是error()
被调用(似乎它甚至在客户端\浏览器接受响应之前就发生了)。
what is wrong?
怎么了?
$(document).ready(function() {
$("#loginForm").submit(function() {
$.ajax({
type : "POST",
data : $("#loginForm :input").not("#loginBtn").serialize(),
url : "http://localhost/auth",
success : function(data, textStatus, jqXHR) {
alert("success");
alert(jqXHR.getResponseHeader("Location"));
},
error : function(jqXHR, textStatus, errorThrown) {
alert("error");
}
});
});
});
回答by xbonez
jquery.post()
works by issuing an Ajax request. In an Ajax call, the PHP script works asynchronously (think of it as a background thread). Thus, the PHP script cannot redirect the client's browser. The way to go would be to have the PHP script responsd with the URL to redirect to, and redirect using javascript.
jquery.post()
通过发出 Ajax 请求来工作。在 Ajax 调用中,PHP 脚本异步工作(将其视为后台线程)。因此,PHP 脚本无法重定向客户端的浏览器。要走的路是让 PHP 脚本响应要重定向到的 URL,并使用 javascript 重定向。
In your POST callback:
在您的 POST 回调中:
function(data){ //data will be the URL to redirect to, sent back by the PHP script
window.location = data;
}
EDIT
编辑
From Jquery documentation on jquery.ajax()
来自jquery.ajax()上的 Jquery 文档
statusCode(added 1.5)Map
statusCode(1.5新增)Map
Default: {}
A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:
当响应具有相应代码时要调用的数字 HTTP 代码和函数的映射。例如,当响应状态为 404 时,以下内容将发出警报:
$.ajax({
statusCode: {
404: function() {
alert('page not found');
}
}
});
If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.
如果请求成功,状态码函数采用与成功回调相同的参数;如果导致错误,则它们采用与错误回调相同的参数。
回答by Deepak Kumar
Please in redirect case get simple response without redirecting print any flag like "redirect" and and write client side redirect using
请在重定向情况下获得简单的响应而不重定向打印任何标志,例如“重定向”并使用编写客户端重定向
if(responce == 'redirect')
window.location = "Your url"
Do not redirect on server.
不要在服务器上重定向。
It will work.
它会起作用。
回答by NibblyPig
If you perform an operation that results in a redirect, JQuery does not detect it. The browser handles it automatically behind the scenes, to create a seamless experience.
如果您执行导致重定向的操作,JQuery 不会检测到它。浏览器会在幕后自动处理它,以创建无缝体验。