我使用的是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")
}
});
}
答案 0 :(得分:1)
success:function(){
//current page URL
var ref = document.URL;
location.href = '/login/?ref=' + ref;
}
您可以使用变量“ref”返回同一页面
答案 1 :(得分:0)
请注意,当您使用ajax调用服务器并接收重定向响应时。浏览器 不会重定向 ,但 会自动检索重定向位置的内容 和您的success
函数将最终从重定向的位置传递内容。对于ajax请求,如果用户未登录,则不应使用重定向进行响应,而应返回401 Unauthorized
,浏览器将在回调中处理此问题。以下是步骤:
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;
}
});
答案 2 :(得分:0)
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>
答案 3 :(得分:0)
<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>