我正在制作这样的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;
});
在服务器端,我做了一些像这样的代码:
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["pid"]))
{
var path = HttpContext.Current.Request.Url.AbsolutePath;
HttpContext.Current.Response.Redirect(path);
}
Ajax POST工作正常.我可以在Mozilla的Web Developer Tools中看到,但是页面没有像我想象的那样被重定向到其他页面.谁能告诉我我做错了什么?
或者是不可能通过Ajax调用Response.Redirect?
解决方法
是的,据我所知,你不能简单地从客户端检测重定向.参考其他答案如下:
> Detecting a redirect in jQuery $.ajax?
> Returning redirect as response to XHR request
您可以做的一件事是如何返回指示服务器端代码重定向的内容.类似下面的JSON:
{
success: true,redirect: true,redirectURL = "http://something.com/path/to/good/stuff"
}
如何在服务器端代码中实现上述功能取决于您.
然后在您的客户端代码中,您可以执行以下操作:
$.ajax({
type: "POST",success: function(data) {
if(data.redirect) {
window.location = data.redirectURL;
}
},