Javascript 通过angularjs发出post请求后,尝试从节点js呈现页面
在angular controller发出post请求后,我尝试从nodejs呈现一个页面。但是,该页面不会加载,而是保持在同一页面上。控制台中没有错误。此外,在浏览器开发工具中,我可以看到加载在网络预览部分下的页面。 这是正确的还是建议的方法?我猜,控件返回到控制器,而不是从节点渲染页面。我们如何避免这种情况,或者有更好的方法来做到这一点? 感谢您的帮助 来自控制器的Post请求:Javascript 通过angularjs发出post请求后,尝试从节点js呈现页面,javascript,jquery,angularjs,node.js,Javascript,Jquery,Angularjs,Node.js,在angular controller发出post请求后,我尝试从nodejs呈现一个页面。但是,该页面不会加载,而是保持在同一页面上。控制台中没有错误。此外,在浏览器开发工具中,我可以看到加载在网络预览部分下的页面。 这是正确的还是建议的方法?我猜,控件返回到控制器,而不是从节点渲染页面。我们如何避免这种情况,或者有更好的方法来做到这一点? 感谢您的帮助 来自控制器的Post请求: $http({ method : "POST",
$http({
method : "POST",
url: '/afterSignin',
data: {
"inputUsername": $scope.inputUsername,
"inputPassword": $scope.inputPassword
}
}).success(function(data)
{
if(data.statusCode==401)
{
$scope.msg = "Invalid Username or Password";
}
else
{
console.log(data.statusCode);
console.log("login successfull");
}
服务器端代码(afterSignIn.js)
控制台输出:
连接已关闭。。
数据库结果:[对象]
有效登录
[0mPOST/afterignin[32m200[0m103.425毫秒-
有许多很好的解决方案。最简单的方法可能是替换Windows的URL:
$http({
method : "POST",
url: '/afterSignin',
data: {
"inputUsername": $scope.inputUsername,
"inputPassword": $scope.inputPassword
}
}).success(function(data){
if(data.statusCode==401) {
$scope.msg = "Invalid Username or Password";
}else {
console.log(data.statusCode);
// Interesting part, redirect the user
$window.location = '/successLogin'
}
});
如您所见,在收到指示登录成功的响应后,您可以在客户端重定向用户。(您必须将$window
作为服务/控制器的依赖项注入。)
更好的解决方案是使用客户端路由器包,例如(您应该走这条路).该渲染将作为对
$http
发出的ajax调用的响应发送。除非您对该响应执行某些操作,否则不会发生任何更改。不太清楚您希望发生什么这是一种登录应用程序。用户登录以及成功登录后,应将完整的用户重定向到主页。是否有其他方法实现此目的s?通常是通过内部应用程序路由器(单页应用程序)完成的角度应用程序。如果您希望完全重新加载页面,则您不会使用$http
提交并使用浏览器默认表单提交过程,或者在成功回调中执行javascript重定向
$http({
method : "POST",
url: '/afterSignin',
data: {
"inputUsername": $scope.inputUsername,
"inputPassword": $scope.inputPassword
}
}).success(function(data){
if(data.statusCode==401) {
$scope.msg = "Invalid Username or Password";
}else {
console.log(data.statusCode);
// Interesting part, redirect the user
$window.location = '/successLogin'
}
});