【问题标题】:How to allow access cross origin requests ??? ( Angular, Rest, Spring )如何允许访问跨源请求??? (角度,休息,弹簧)
【发布时间】:2018-06-19 10:23:27
【问题描述】:

我需要帮助

我正在使用 spring 框架开发一个使用 angular 和 restful web 服务的应用程序

从 RESTController 调用此方法后

 @CrossOrigin(origins = "http://localhost:4200")
 @RequestMapping(value="/search",method=RequestMethod.GET)
 public Page<Bien> chercher(
        @RequestParam(name="mc",defaultValue="") String mc,
        @RequestParam(name="size",defaultValue="0") int page,
        @RequestParam(name="page",defaultValue="5") int size) {
    return bienRepository.chercher("%"+mc+"%",new PageRequest(page,size));
}

使用

 this.http.get('url:http://localhost:8080/searchmc=A').
 map(resp=>resp.json()).
 subscribe(data=>{this.pageBien=data;},err=>{console.log(err) } );

我遇到了这个问题

无法加载 url:http://localhost:8080/search?mc=A: 跨域请求仅支持协议方案:http、data、chrome、chrome-extension、https。

请给点建议!

【问题讨论】:

  • 是否需要url:开头。从错误消息看来,这就是问题所在。只使用http://.... 而不使用url:

标签: spring angular rest


【解决方案1】:

您从客户端获取的请求似乎不正确。 正确的应该是:

this.http.get('http://localhost:8080/search?mc=A').map(resp=>resp.json()).
subscribe(data=>{this.pageBien=data;},err=>{console.log(err) } );

发生了什么变化:

  1. 在 get 调用中删除“url”前缀
  2. 您将请求参数包含在 url 中的 ? 之后,因此 http://localhost:8080/search?mc=A(对于每个下一个请求参数使用 &,例如 http://localhost:8080/search?mc=A&size=5

【讨论】:

    猜你喜欢
    • 2018-04-27
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2015-04-15
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多