Here is my HTML form
<form class="navbar-form navbar-right" ng-submit="loginuser()">
<input type="email" ng-model="user.username" placeholder="Email" class="form-control" required>
<input type="password" ng-model="user.password" placeholder="Password" class="form-control" required>
<button class="btn btn-success" type="submit">Login</button>
</form>
and my app.js is
function navbarformcontroller($scope,$http){
$scope.user = {};
$scope.loginuser = function(){
console.log('entered');
$http({
method : 'POST' ,
url : 'http://localhost:3000/login',
data : $scope.user
})
.success(function(data){
alert(data);
})
.error(function(data, status){
alert(data + " " + status + 'x');
})
};
}
and on my server side is node js is
router.get('/', function(req, res) {
req.render('login',{username : req.username, password : req.password});
res.send('success');
log(username + " " + password);
});
and error details are in client console is
XMLHttpRequest cannot load
http://localhost:3000/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
Your browser is restricting access to a different domain. You are using localhost however different ports thats why your domain is getting different.
The best solution over here is to run the server and request access using the same port. Since you running the nodejs server on port 63342 you need to access /login page with the domain
http://localhost:63342
Better still since the domain is the same you can avoid adding the domain and have the request as
function navbarformcontroller($scope,$http){
$scope.user = {};
$scope.loginuser = function(){
console.log('entered');
$http({
method : 'POST' ,
url : '/login',
data : $scope.user
})
.success(function(data){
alert(data);
})
.error(function(data, status){
alert(data + " " + status + 'x');
})
};
}
There's a few issues here:
It seems like you may not be using a body parsing middleware. If you're not dealing with files, you could probably just use the body-parser module on npm. If you need multipart/form-data (file) support, there are links to modules that can handle these types of requests, in body-parser's readme.
You're trying to both render a template and the string "success" to the client simultaneously. Also, for template rendering it's res.render() not req.render().
You're attempting a cross-origin request. While the hostname may be the same, the ports are different and that also constitutes a cross-origin request. So one of the easiest ways to handle CORS in Express is to use a middleware such as cors on npm. If you need to send cookies, custom headers, etc. with the request, you'll need to set withCredentials: true in your $http config object.