我正在尝试在用户点击登录按钮后将用户重定向到成功屏幕。一切似乎工作正常,但页面不会改变。尝试在线搜索,但没有一个解决方案有帮助。
更新似乎根本原因是由于路由中的“/”。将其更改为“/ home”,现在可以使用
解决
以下是我的HTML,服务器和路线代码
的index.html
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../public/css/style.css">
</head>
<body>
<div ng-app="myApp" ng-controller="loginController">
<div class="container">
<br>
<br>
<br>
<br>
<h1 class="welcome text-center" style="text-align:center;">Welcome</h1>
<div class="card card-container">
<!--
<h2 class='login_title text-center'>Login</h2>
-->
<hr>
<form class="form-signin">
<p class="input_title">Username:</p>
<input type="text" id="inputEmail" class="login_box" ng-model="username" required autofocus>
<p class="input_title">Password</p>
<input type="password" id="inputPassword" class="login_box" ng-model="password" required>
<br>
<br>
<button class="btn btn-lg btn-primary" type="submit" ng-click="sub()">Login</button>
</form><!-- /form -->
</div><!-- /card-container -->
</div><!-- /container -->
</div>
<script>
var app = angular.module('myApp', []);
app.controller("loginController", function($scope,$http) {
$scope.sub = function() {
var data = {
username: $scope.username,
password: $scope.password
}
var config = {
headers : {
'Content-Type': 'application/x-www-form-
urlencoded;charset=utf-8;'
}
}
$http.post('/login', data, config)
.then(
function(response){
console.log('action on success');
},
function(response){
console.log('action on error');
}
);
}
});
</script>
</body>
</html>
server.js
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var config = require('./config');
var app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(express.static(__dirname + '/app'));
require('./app/routes')(app);
app.listen(3000,function(err){
if(err){
console.log(err);
}
else{
console.log("Listening on port 3000");
}
});
route.js
module.exports = function (app) {
app.get('/',function(req,res){
res.sendFile(__dirname + '/views/index.html');
});
app.post('/login', function(req, res){
console.log("post here!");
res.setHeader("Content-Type", "text/html")
res.sendFile(__dirname + '/views/success.html');
//res.redirect(__dirname + '/app/views/success.html');
res.end;
});
答案 0 :(得分:1)
以下是我的建议,
如果您尝试在路线文件中重定向,请使用:
return res.redirect('your url');
如果您尝试重定向ajax文件,请使用:
window.location.href = 'your url';