jQuery 通过 Ajax 调用 Response.redirect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19762629/
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
Calling Response.redirect through Ajax
提问by None
I am making an Ajax request like this:
我正在发出这样的 Ajax 请求:
$(".box01 .selproduct").live("click", function(e) {
var color = $(this).parent('.box01').find('.color').val();
var size = $(this).parent('.box01').find('.size').val();
var pid=$(this).parent('.box01').find('.hdinput').val();
var pathname = window.location.pathname;
var data = { submit: "selected",size:size,color:color,pid: pid};
$.ajax({
type: "POST",
url: pathname,
data: data,
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
complete: function(data) {
}
});
return false;
});
And in the server side I have done some code like this:
在服务器端,我做了一些这样的代码:
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["pid"]))
{
var path = HttpContext.Current.Request.Url.AbsolutePath;
HttpContext.Current.Response.Redirect(path);
}
Ajax POST works fine. I can see in Web Developer Tools in mozilla but page is not redirected to other page as I supposed. Can any one tell me what I am doing wrong?
Ajax POST 工作正常。我可以在 mozilla 的 Web Developer Tools 中看到,但页面没有像我想象的那样重定向到其他页面。谁能告诉我我做错了什么?
Or Is it not possible to call Response.Redirect
through Ajax?
还是Response.Redirect
不能通过 Ajax调用?
回答by Patrick Jones
Yeah, to my knowledge you can't simply detect the redirect from the client-side. Reference other answers like these:
是的,据我所知,您不能简单地从客户端检测重定向。参考其他这样的答案:
One thing you can do is how return something that indicates a redirect from your server-side code. Something like the following JSON:
您可以做的一件事是如何从服务器端代码返回指示重定向的内容。类似于以下 JSON:
{
success: true,
redirect: true,
redirectURL = "http://something.com/path/to/good/stuff"
}
How you achieve the above in your server-side code is up to you.
如何在服务器端代码中实现上述目标取决于您。
Then in your client-side code you can do the following:
然后在您的客户端代码中,您可以执行以下操作:
$.ajax({
type: "POST",
url: pathname,
data: data,
success: function(data) {
if(data.redirect) {
window.location = data.redirectURL;
}
},
回答by Subin Jacob
It is impossible to call Response.Redirect in WebMethod. Instead you can use
在 WebMethod 中调用 Response.Redirect 是不可能的。相反,您可以使用
success: function(data) {
window.location.href="path.aspx";
}
in ajax Success function.
在 ajax 成功函数中。
If the page name is dynamic in nature return pagename from webmethod and use it to redirect the page.
如果页面名称本质上是动态的,则从 webmethod 返回 pagename 并使用它来重定向页面。