Calling Response.redirect through Ajax

后端 未结 2 1656
有刺的猬
有刺的猬 2020-12-11 05:02

I am making an Ajax request like this:

 $(\".box01 .selproduct\").live(\"click\", function(e) {
    var color = $(this).parent(\'.box01\').find(\'.color\').v         


        
相关标签:
2条回答
  • 2020-12-11 05:03

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

    • Detecting a redirect in jQuery $.ajax?
    • Returning redirect as response to XHR request

    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"
    }
    

    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;
          }
        },
    
    0 讨论(0)
  • 2020-12-11 05:03

    It is impossible to call Response.Redirect in WebMethod. Instead you can use

     success: function(data) {
         window.location.href="path.aspx";
            }
    

    in ajax Success function.

    If the page name is dynamic in nature return pagename from webmethod and use it to redirect the page.

    0 讨论(0)
提交回复
热议问题