Reputation: 37
I have Restfull API (localhost:8180) which is having secure authentication. Using Angular JS http (or other services) I need to login into that server and get my data from localhost:8180/api/version/?1 (this is just example) Please find my code below.
//configure file
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
}]);
//setting cookie
app.run(['$http', '$cookies', function($http, $cookies) {
$http.defaults.headers.post['JSESSIONID'] = $cookies.JSESSIONID;
}]);
// My Controller
$http({
url:'http://localhost:8180',
method:'POST',
data: {
username:'adminadmin',
password:'passpass'
},
headers: {
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Credentials": true,
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept": "*/*",
'Authorization': 'Basic adminadmin:passpass' // I guess its wrong
}
})
.success (function(){
console.log("Success");
//Need to call my API here
})
.error(function(data, status, headers, config){
console.log("Failed");
});
Here I'm getting No Access-Control-Allow-Origin and Origin null error. If success I need to call localhost:8180/api/version?1 inside of the success method using get or post. Here my server is localhost:9596. I need to call 8180 host from 9596 host and get the data into 9596 host.
Upvotes: 1
Views: 1227
Reputation: 304
Whenever you are getting Cross-origin Error, you need to as the server guys to add some headers sent while sending responses back to the ajax request.
Basing on Backend you need to use different approach to making this change in server:
In Java Tomcat Server: You need to configure filter mapping
Source : http://enable-cors.org/server.html
Upvotes: 1