java 如何在jquery回调函数中重定向到新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5881563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect to a new page in jquery callback function
提问by
I am developing an application using jquery along with servlets.I have been using jquery theme roller for interface In my Login.jsp
我正在使用 jquery 和 servlets 开发一个应用程序。我一直在使用 jquery 主题滚轮作为界面在我的 Login.jsp 中
<html>
<head>
<script>
$(document).ready(function() {
$("#dialog").dialog();
});
</script>
<script>
$("#submit").click(function(){
$("#LoginForm").submit(function()
{
var username=$("#username").val();
var password=$("#password").val();
var datastring='username='+username+ '&password= '+password;
$.ajax({
type: "POST",
url:'Login',
data: datastring
error: function(){
alert("Data Error");
},
success: function (data){
window.location.replace("Items.jsp");
}
});
});
});
</script>
</head>
<body style="font-size:62.5%;">
<div id="dialog" title="Login">
<form id="LoginForm" method="post">
<fieldset>
<label>Username:</label>
<input type="text" id="username"></input><br></br>
<label>Password:</label>
<input type="password" id="pwd"></input><br></br>
<input type="submit" id="submit" value="Log In" align="middle"></input>
</fieldset>
</form>
</div>
The data is being passed to the servlet and once the login is successful i want the user to be redirect to a new page say Items.jsp. In my callback I have used window.location.replace("Items.jsp").I am not able to redirect.
数据被传递到 servlet,一旦登录成功,我希望用户被重定向到一个新页面,比如 Items.jsp。在我的回调中,我使用了window.location.replace("Items.jsp")。我无法重定向。
I tried with response.sendRedirect("Items.jsp")and return *"Items.jsp" *
我尝试使用response.sendRedirect("Items.jsp")并返回 * "Items.jsp" *
But am Not able to Redirect it to a new page.Where I am goin wrong..
但是我无法将其重定向到新页面。我哪里出错了..
Thanks:)
谢谢:)
回答by netbrain
Have you tried window.location = "items.jsp"
?
你试过window.location = "items.jsp"
吗?
EDIT
编辑
I see you are missing a "url:" parameter in your ajax call.
我看到您在 ajax 调用中缺少“url:”参数。
Look at my example here which works.
看看我的例子here有效。
回答by suren
Setting window.location to new URL will change the current webpage to the specified location.
将 window.location 设置为新 URL 会将当前网页更改为指定位置。
window.location = "http://www.stackoverflow.com/"
回答by Suhail
Use this :
$(window.location).attr('href', 'http://www.google.com');
回答by naveen
Try window.location.href ="Items.jsp"
尝试 window.location.href ="Items.jsp"
Source: window.location MDC