I have tried accepted answers in stackoverflow for this problem.
In angularjs,when i send a post
request to local Api
it works.but when i change to cross domain api it does not work.
This is my code
var myapp = angular.module('myApp',['ngRoute','http-auth-interceptor'])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
.factory('featuresData', function ($http) {
return{
doCrossDomainGet: function(data) {
return $http({
url:'http://example.com/api/login',
type: "POST",
crossDomain: true,
data: JSON.stringify({"username":"test_teacher" , "password":"123"}),
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (response) {
console.log(response);
},
error: function (xhr, status) {
alert("error");
}
})
}
}
});
I am getting this message in console:
XMLHttpRequest cannot load example.com/api/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost'; is therefore not allowed access. The response had HTTP status code 405.
The Api works fine from Android,Ios
client apps and from CURL
curl command
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://example.com/api/login -d '{"username":"test_teacher","password":"123"}'
curl output
{"username":"test_teacher","roles":["ROLE_TEACHER"],"access_token":"7q8dcreo5bngk32jq8nmefrfgp6nope2"}