我有Restfull API(localhost:8180),它具有安全身份验证。使用Angular JS http(或其他服务)我需要登录到该服务器并从localhost获取我的数据:8180 / api / version /?1(这只是示例)请在下面找到我的代码。
//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");
});
我在这里获得No Access-Control-Allow-Origin和Origin null 错误。如果成功,我需要调用localhost:8180 / api / version?1 使用get或post的成功方法。我的服务器在这里 本地主机:9596。我需要从9596主机拨打8180主机并获得 数据进入9596主机。
答案 0 :(得分:1)
每当您收到跨源错误时,您需要作为服务器人员添加一些在将响应发送回ajax请求时发送的标头。
基于后端,您需要使用不同的方法在服务器中进行此更改:
在Java Tomcat服务器中:您需要配置过滤器映射