[英]Redirect to login page onajax request
我正在使用jsp,jquery和struts。 我在理解ajax请求的重定向到登录页面时遇到问题。 我试图在XHR选项卡上的浏览器上看到请求,它在标头中提供了302状态代码。 我无法理解如何重定向。 我的方法
登录后如何返回同一页面? 有什么办法吗? 同样对于服务器端的重定向,我正在使用Response.redirect()。 当我调试代码并且响应出现在客户端时,ajax函数中的错误函数被执行而不是成功函数。 有人可以解释如何捕获服务器的响应吗?
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;
}
您可以使用变量“ ref”返回同一页面
请注意,当您使用ajax来调用服务器并收到重定向响应时。 浏览器不会重定向 ,但会在重定向位置自动检索内容,并且您的success
功能最终将从重定向位置传递内容。 对于ajax请求,如果用户未登录,则不应使用重定向响应,而应返回401 Unauthorized
,浏览器将在回调中处理此问题。 步骤如下:
X-Requested-With: XMLHttpRequest
的标头X-Requested-With: XMLHttpRequest
(这表示ajax请求) 然后您的ajax回调可以检查是否存在401响应,将location.href
设置为登录页面。 您可以在登录页面中输入类似returnUrl
查询字符串参数的内容,如果可以的话,可以将其设置为当前页面以在登录后重定向到该页面。 客户端示例代码:
$.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('#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.