[英]Redirect to login page onajax request
I am using jsp, jquery, and struts. 我正在使用jsp,jquery和struts。 I have a problem understanding the redirect to login page for ajax request.
我在理解ajax请求的重定向到登录页面时遇到问题。 I tried to see request on browser on XHR tab and it gives me 302 status code in header.
我试图在XHR选项卡上的浏览器上看到请求,它在标头中提供了302状态代码。 I am not able to comprehend how do I redirect.
我无法理解如何重定向。 My approach
我的方法
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().
同样对于服务器端的重定向,我正在使用Response.redirect()。 When I debug the code and response comes on client side the error function in ajax function is executed not success function.
当我调试代码并且响应出现在客户端时,ajax函数中的错误函数被执行而不是成功函数。 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")
}
});
}
success:function(){
//current page URL
var ref = document.URL;
location.href = '/login/?ref=' + ref;
}
You can use variable "ref" to come back the same page 您可以使用变量“ ref”返回同一页面
Note that when you use ajax to call server and receive a redirect response. 请注意,当您使用ajax来调用服务器并收到重定向响应时。 The browser will not redirect , but will automatically retrieve the content at redirected location and your
success
function will end up being passed the content from the redirected location. 浏览器不会重定向 ,但会在重定向位置自动检索内容,并且您的
success
功能最终将从重定向位置传递内容。 For ajax request, you should not respond with a redirect if the user is not signed in, you should return a 401 Unauthorized
instead and the browser will handle this in your callback. 对于ajax请求,如果用户未登录,则不应使用重定向响应,而应返回
401 Unauthorized
,浏览器将在回调中处理此问题。 Here are the steps: 步骤如下:
X-Requested-With: XMLHttpRequest
(this indicates an ajax request) X-Requested-With: XMLHttpRequest
的标头X-Requested-With: XMLHttpRequest
(这表示ajax请求) And your ajax callback could check if there is a 401 response, set location.href
to your login page. 然后您的ajax回调可以检查是否存在401响应,将
location.href
设置为登录页面。 You could have something like returnUrl
query string parameter in your login page, if there is you could set it to your current page to redirect to this page after login. 您可以在登录页面中输入类似
returnUrl
查询字符串参数的内容,如果可以的话,可以将其设置为当前页面以在登录后重定向到该页面。 Sample code on client side: 客户端示例代码:
$.ajax({
type:"GET",
data:{
X:param1,
Y:param2,
},
url:"/application",
success:function()
{
alert("success message");
},
error:function()
{
alert("error message")
},
statusCode: {
401: function() {
location.href = '/login/?returnUrl=' + document.URL;
}
});
jQuery(document).ready(function () { jQuery(document).ready(function(){
jQuery('#dialog').show(false);
jQuery('#MsgDel').show(false);
jQuery("#dialog").dialog({
bgiframe: true, autoOpen: false, height: 250, modal: true, width: 250,
buttons: {
"Delete": function () {
var txtValue = $("#<%=txtid.ClientID %>").val();
var txtrea = $("#<%=txtreason.ClientID %>").val();
if (txtrea == '') {
}
else {
Delete(txtValue, txtrea);
$(this).dialog("close");
}
},
"Cancel": function () {
$(this).dialog("close");
}
},
close: function () {
$('#txtreason').val("");
}
});
});
</script>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#MsgDel').show(false);
jQuery("#MsgDel").dialog({
bgiframe: true, autoOpen: false, height: 150, modal: true, width: 300,
buttons: {
"OK": function () {
$(this).dialog("close");
--To Reload Page Again After Success Function
window.location.href = "DispatchInstructionList.aspx";
}
},
close: function () {
}
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.