启动应用项目:春云 - Zuul代理是生产无“访问控制允许来源” Ajax响应
@SpringBootApplication
@EnableZuulProxy
public class ZuulServer {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulServer.class).web(true).run(args);
}
}
我YAML文件是这样的:
server:
port:8080
spring:
application:
name: zuul
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
proxy:
route:
springapp: /springapp
我有一个微服务应用程序(在端口8081上)称为springapp并有一些休息服务。下面是我的客户端UI应用程序:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/libs/jquery/jquery.min.js" ></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
url: 'http://localhost:8080/zuul/springapp/departments',
type: 'GET'
}).done(function (data) {
consoe.log(data);
document.write(data);
});
</script>
</body>
</html>
但我得到一个
XMLHttpRequest cannot load http://localhost:8080/zuul/springapp/departments. No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8383' is therefore not allowed access.
这个UI HTML5应用是http://localhost:8383/SimpleAPp/index.html。 CORS,CORS,CORS ...请帮助。顺便说一句,http://localhost:8080/zuul/springapp/departments返回一个json列表,假设在浏览器地址栏上。 spring.io博客here表示不需要过滤器,因为zuulproxy处理这个问题,但我不知道为什么它不适合我。
感谢试图回答,但我加了标签的原因是因为来自Spring云的@EnableZuulProxy。在spring.io的这篇文章[博客](http:// spring。io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv)他们说没有必要使用你提到的Filter,因为ZuulProxy做了一个消费客户端的反向代理。希望这是有道理的。 – 2015-02-23 14:00:03
是的,但正如下面的@zeroflagL所说,这意味着您必须通过代理发送所有请求。 – 2015-02-23 15:21:24