通过Ajax的调用的Response.RedirectAjax、Response、Redirect

2023-09-10 17:18:17 作者:到此为止

我想提出一个Ajax请求像这样

I am making an Ajax request like this

 $(".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;
});

而在服务器端的我做了一些code这样

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);
    }

阿贾克斯自检工作正常,我可以在webdeveloper工具看到了Mozilla,但页面不重定向到其他页面,我supposed.Can任何一个可以告诉我,我做错了什么?

Ajax POST works fine that i can see in webdeveloper tools in mozilla but page is not redirected to other page as i supposed.Can any one tell me what I am doing wrong?

或者是无法调用的Response.Redirect 通过Ajax的?

Or Is it not possible to call Response.Redirect through Ajax?

推荐答案

是啊,据我所知,你不能简单地检测来自客户端的重定向。参考其他答案这样的:

Yeah, to my knowledge you can't simply detect the redirect from the client-side. Reference other answers like these:

检测jQuery的$就重定向? Returning重定向作为响应XHR请求 Detecting a redirect in jQuery $.ajax? Returning redirect as response to XHR request

一件事你可以做的是如何返回的东西,指示从服务器端code重定向。类似如下的JSON:

One thing you can do is how return something that indicates a redirect from your server-side code. Something like the following JSON:

{
  success: true,
  redirect: true,
  redirectURL = "http://something.com/path/to/good/stuff"
}

您如何实现上述在服务器端code是由你。

How you achieve the above in your server-side code is up to you.

然后在您的客户端code,你可以做到以下几点:

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;
      }
    },