Javascript 如何在jquery帖子的回调中重定向
我的主页在收到数据后不会重定向到登录页面。 以下是主页:Javascript 如何在jquery帖子的回调中重定向,javascript,jquery,redirect,callback,routing,Javascript,Jquery,Redirect,Callback,Routing,我的主页在收到数据后不会重定向到登录页面。 以下是主页: $(function(){ const submit = $('#submit'); const save = $('#save'); const email = $('#email'); const username = $('#username'); const password = $('#password'); const userlogin = $('#Username login
$(function(){
const submit = $('#submit');
const save = $('#save');
const email = $('#email');
const username = $('#username');
const password = $('#password');
const userlogin = $('#Username login');
const passlogin = $('#Password login');
save.click(function(e){
e.preventDefault();
$.post('/signup',{
email:email.val(),
username:username.val(),
password:password.val()
}, function(data){
console.log(1)
if(data.username){
localStorage.setItem("username",data.username)
}
window.location.href(data.url)
})
})
submit.click(function(e){
e.preventDefault();
$.post('/login',{
username:userlogin.val(),
password:passlogin.val()
}, function(data){
window.location.href(data.url)
})
})
})
这是后端上存在的routes.js文件
app.post('/signup', function(req,res){
var a = [req.body.email,req.body.username,req.body.password];
Users.findOne({
where: { email: a[0],
username: a[1],
password: a[2],
}
}).then(users => {
if (users) {
res.send({
url:'/'
})
}
else{
Users.create({
email: a[0],
username: a[1],
password: a[2],
}).then(users => {
res.send({
url:'/profile',
username: a[1]
})
})
}
})
})
收到来自服务器的成功回调后,我需要重定向到/profile。但是现在页面没有加载,但是用户名字段成功存储在localstorage中。
我相信这是由于window.location.href的
.href
有一个window.location
属性,您可以获取/设置,而不是函数本身(从脚本编写者的角度)。请尝试以下方法:
window.location.href = data.url;
你可以用多种方式来做 首先让我告诉你你的代码有什么问题 你必须使用
window.location.href = "https://www.example.com";
因为你写的符号是错误的
window.location.href(data.url) // this is wrong
现在来看看另一条路
您可以在标记中以及在“操作”属性中指定post方法
<form name="some_name" method="POST" action="/signup">
... Your Form ...
</form>
... 你的表格。。。
Sure;)如果你面临任何问题,请告诉我。