【发布时间】:2019-03-04 10:58:59
【问题描述】:
我正在创建一个资源监视器,用于检索有关不同域但相同端口上的机器的典型信息。直接访问url时,数据返回成功。但是,如果我们使用 angularJs 尝试它,$http.get 请求将返回“被 CORS 策略阻止:请求的资源上不存在‘Access-Control-Allow-Origin’标头”。我们决定使用 chrome CORS 扩展来允许连接。现在唯一的问题是 $http.get 请求始终为空,尽管数据存在。不知道为什么会发生这种情况,因为没有产生错误。
角度控制器
app.controller("ServerResourcesController", [ "$scope", "$http", function($scope, $http) {
$http.get("http://000.000.0.0:8080/testing")
.then(function(data){
console.log(data);
})
}]);
控制器
@RestController
public class ServerRestController {
Logger logger = LoggerFactory.getLogger(ServerRestController.class);
ServerQueryController sqc = new ServerQueryController();
@RequestMapping("/server-service-info")
public String ServiceInfo() {//Welcome page, non-rest
return "Server Resource Monitor Service";
}
//rest end point
@GetMapping("/server-resources-info")
public ServerInformation ServerInformation() {
ServerInformation serverInformation = sqc.CurrentServerResourceInformation();
return serverInformation;
}
}
对象类
@Getter @Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class ServerInformation {
private String name;
private String ipAddress;
private double systemCpuLoad;
private double freePhysicalMemory;
private double totalPhysicalMemory;
private String operatingSystem;
private double freeDiskSpace;
private double diskUsage;
public ServerInformation() {
}
@Override
public String toString() {
return "Values{ systemCpuLoad: "+systemCpuLoad+
", freePhysicalMemory: "+freePhysicalMemory+
", totalPhysicalMemory: "+totalPhysicalMemory+
", operatingSystem: "+operatingSystem+
", freeDiskSpace: "+freeDiskSpace+
", diskUsage: "+diskUsage+
" }";
}
}
【问题讨论】: