ng
function loginController($scope, $http) {
$scope.login = function () {
var url = 'http://localhost:3000/login';
var data = {username : $scope.username,
password: $scope.password
}
$http.post(url, data).success(function (data, status, headers, config) {
console.log(data);
if (data.flag === 'ok') {
$scope.flag = '登陆成功';
}else {
$scope.flag = '登陆失败';
}
}).error(function (data, status, headers, config) {
})
}
}
Node
var app = require('express')();
var bodyParser = require('body-parser');
app.all('*',function(req,res,next)
{
if (!req.get('Origin')) return next();
res.set('Access-Control-Allow-Origin','*');
res.set('Access-Control-Allow-Methods','GET,POST');
res.set('Access-Control-Allow-Headers','X-Requested-With,Content-Type');
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/login', function (req, res, next) {
console.log(req.body);
if (req.body.username === 'yy' && req.body.password === 'yyy') {
res.json({flag:'ok'});
}else {
res.json({flag:'fail'});
}
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});