I am running my frontend in Node js and backend in Jetty.I am trying to post data from frontend using angular JS but its not working for me .Can anyone suggest what could be the issue? Node Js running in port 3000 and jetty running in 9011. Here is the code snippet:
var app = angular.module("loginapp", []);
app.controller("loginctrl", function($scope,$http) {
app.config(['$sceDelegateProvider', function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist(['self', 'http://localhost:9011/**']);
}]);
$scope.login = function(e){
console.log('clicked login...');
e.preventDefault();
$http({
url: "http://localhost:9011/test/dummy/doStuff1",
method: "POST",
data : {username: $scope.userName, password: $scope.password},
headers: { 'Content-Type': 'application/json' },
withCredentials: false,
}).success(function (data, status, headers, config) {
console.log('status',status);
console.log('data',status);
console.log('headers',status);
});
}
});
In the browser console it shows:
POST http://localhost:9011/test/dummy/doStuff1
(index):1 XMLHttpRequest cannot load http://localhost:9011/test/dummy/doStuff1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 500.
I am not sure what causing the issue can someone help me on this?
If you are hosting your angular app on node(localhost:3000) and trying to make an ajax request to jetty(localhost:9011) you are going to have CORS(http://enable-cors.org/) issues. By default the browser won't let you make a request to a different domain. You need to enable CORS on the server and in your angular app.
Server side obviously depends on the framework you're using. An example using Jersey: http://blog.usul.org/cors-compliant-rest-api-with-jersey-and-containerresponsefilter/
Then enable CORS in Angular(this may not be necessary in your case... it is required if you load templates from a different domain):
//Allow CORS requests to these urls:
$sceDelegateProvider.resourceUrlWhitelist([
'self',
'http://localhost:9011/**'
]);