简体   繁体   English

在Java和jquery中重定向Ajax调用

[英]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. 我正在使用jsp和jquery ...以及struts。我在理解重定向到ajax请求的登录页面时遇到问题。我试图在XHR选项卡上的浏览器上查看请求,它在标头中提供了302状态代码。我无法了解我如何重定向。 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. 我的方法1)应用程序具有检查用户是否登录的功能,并具有重定向到登录URL的功能。 2)Else do some other processing. 2)其他进行其他处理。

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?... 登录后如何返回同一页面?有什么办法?..也用于服务器端重定向,我也使用Response.redirect()...有人可以解释如何从服务器捕获响应吗。 。

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. 使用传递给ajax success()回调的data参数。 If a 302 was issued, the target URL would be available at data.redirect . 如果发出302 ,则目标URL将在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';
      }
    }        
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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