简体   繁体   中英

redirect ajax call in java and jquery

I am using jsp and jquery...and struts.I have a problem understanding the redirect to login page for ajax request.I tried to see request on browser on XHR tab and it gives me 302 status code in header.I am not able to comprehend how do i redirect. My approach 1)The application has a function which checks if the user is signed in or not and has function to redirect to login url. 2)Else do some other processing.

How do I come back to same page after login.Is there any way?.....Also for redirecting on server side i am using Response.redirect()...Can someone explain how to catch response from server?...

function buttonpress(param1,param2)
{
$.ajax({
type:"GET",
data:{
X:param1,
Y:param2,
},
url:"/application",
success:function()
{
alert("success message");
}
error:function()
{
alert("error message")
}

});
}

Use the data parameter passed to the ajax success() callback. If a 302 was issued, the target URL would be available at data.redirect .

function buttonpress(param1,param2)
{
  $.ajax({
    type: "GET",
    data: {
      X:param1,
      Y:param2,
    },
    url: "/application",
    success: function(data, status)
    {
      if (data.redirect) {
        window.location.href = data.redirect;
      } else {
        alert("success message");
      }
    },
    error:function()
    {
      alert("error message")
    }
  });
}

you can try this

function buttonpress(param1,param2)
{
  $.ajax({
    type: "GET",
    data: {
      X:param1,
      Y:param2,
    },
    url: "/application",
    success: function(data)
    {
      var flag = data.trim();
      if (flag) {
         // success code
      } else {
         window.location.href = '/logout.do';
      }
    }        
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM