I'm trying to recive data from REST API using POST. I created controller that should make POST request.
function LoginCtrl($scope, $http, $rootScope) {
$scope.login = function () {
var form = {};
form.user = $scope.name;
form.pwd = $scope.password;
$http({
method: 'POST',
url: serverUrl+'/login',
data: $.param(form),
headers: {
'Content-type': 'application/json'
}
})
.success(function(data, status, headers, config) {
//success
})
.error(function(data, status, headers, config) {
//error
});
}
When I run this code, browser creates OPTIONS request and show Access-Control-Allow-Origin (see below). My API only work with POST requests and require Content-type header set to application/json.
But when I change Content-type to application/x-www-form-urlencoded browser make POST request, but still I see error:
XMLHttpRequest cannot load http://nimoz.asi.pl:8080/api/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
How to set Content-type header to application/json, keep POST method and avoid Access-Control-Allow-Origin error?
I use AngularJS version 1.2.10.
EDIT: When I test API on DEV HTTP CLIENT (Chrome extension), request work: http://scr.hu/277/hfwby