gpt4 book ai didi

java - Spring 启动 AngularJs $http.get 为空

转载 作者:可可西里 更新时间:2023-11-01 16:35:40 28 4
gpt4 key购买 nike

我正在创建一个资源监视器,用于检索有关不同域但相同端口上的机器的典型信息。直接访问url时返回数据成功。但是,如果我们使用 angularJs 尝试它,$http.get 请求将返回“被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header ”。我们决定使用 chrome CORS 扩展来允许连接。唯一的问题是,尽管数据存在,但 $http.get 请求始终为空。不确定为什么会发生这种情况,因为没有产生错误。

Angular Controller

app.controller("ServerResourcesController", [ "$scope", "$http", function($scope, $http) {

$http.get("http://000.000.0.0:8080/testing")
.then(function(data){
console.log(data);
})
}]);

Controller

@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+
" }";
}

}

最佳答案

看来你的 ServerRestController 需要有跨源,添加这个

@RestController
@CrossOrigin(origins = "*")
public class ServerRestController {
...
}

另外,如果你想允许一个特定的来源,你可以这样做:

@CrossOrigin(origins = "http://stackoverflow.com", maxAge = 3600)

您可以在每个方法或 RestController 上设置 @CrossOrigin 允许的起点和最大年龄。

此外,如果您有多个 RestController,最好在每个 Controller 上都编写 @CrossOrigin,您可以创建一个 Filter 像这样:

@Component
public class SimpleCORSFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);

public SimpleCORSFilter() {
log.info("SimpleCORSFilter init");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

请参阅此处示例:spring cors

关于java - Spring 启动 AngularJs $http.get 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54981843/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com