【发布时间】:2023-04-04 10:06:02
【问题描述】:
我正在尝试创建一个网络服务。我在客户端使用 AngularJS,在 Web 服务中使用 Java。这是我的客户端代码
var url = 'http://localhost:8010/something';
var data = {
"departmentID":$scope.departmentID ,
"departmentName":$scope.departmentName,
};
var config = {
headers: {
'Content-Type': 'Application/JSON',
},
withCredentials : true
};
$http.put(url,data).success(
function(data, status, headers, config){
alert("success :"+data);
}).error(
function(data, status, headers, config){
alert("Failure :"+data);
}
);
这是我的服务器端代码
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response add(final Department department) {
try {
DepartmentRelation.addDepartment(department);
return Response.status(Status.OK).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").header("Access-Control-Allow-Credentials",true).build();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
return Response.status(Status.INTERNAL_SERVER_ERROR).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();
} catch (SQLException e) {
e.printStackTrace();
return Response.status(Status.BAD_REQUEST).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();
}
}
但我收到此错误 - “请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问 Origin 'http://localhost'。”当我尝试使用它时。
【问题讨论】:
标签: angularjs web-services jakarta-ee cors