2016-11-04 47 views
0

我使用角2开发的应用程序,我发现下面的错误,当我尝试调用API:否“访问控制允许来源”标头角2,但卷曲工作正常

XMLHttpRequest无法加载http://localhost:9090/conf。对预检请求的响应不会通过访问控制检查:请求的资源上不存在“访问控制 - 允许来源”标头。因此'Origin'http://localhost:5555'不允许访问。

在这里,有我的电话的两个例子:

import { Http, Response } from '@angular/http'; 

// ... 

@Injectable() 
export class ConfService { 

    // ... 

    getConf(): Observable<string[]> { 
    return this.http.get(
         'http:localhost:9090/conf?id=1', 
         { 
         headers: { 
          'Accept': 'application/json;charset=UTF-8' 
         } 
         } 
        ) 
        .map((res: Response) => res.json()) 
        .catch(this.handleError); 
    } 

    setConf(conf: any[]): Observable<string[]> { 
    return this.http.get(
         'http:localhost:9090/conf?id=1', 
         conf, 
         { 
         headers: { 
          'Content-Type': 'application/json;charset=UTF-8' 
         } 
         } 
        ) 
        .map((res: Response) => res.json()) 
        .catch(this.handleError); 
    } 

} 

然而,当我使用卷曲它的作品!

$ curl -XPUT 'localhost:9090/configuration?id=1' \ 
> -H 'Content-Type: application/json' 
> -d '{"conf":1234}' 
Success! 
$ curl -XGET 'localhost:9090/conf?id=1' 
{"conf":1234} 

这里发生了什么事?

谢谢

+1

https://chrome.google.com/webstore/detail/cors-toggle/omcncfnpmcabckcddookmnajignpffnh – Lonely

回答

3

这是因为浏览器的安全性,在浏览器中的一般CORS问题。您的Web应用程序在localhost:5090上运行,服务器在localhost:9090上运行。浏览器将不允许访问具有不同端口号的另一台主机。解决方案是处理您的服务器中的CORS(即在您的应用程序运行在localhost:9090)

+0

我的Spring应用程序使用Tomcat和I如果有另一个应用程序(我的Angular客户端)使用相同的端口,则无法启动它。 –

+0

当然,他们必须在不同的港口运行。配置你的春天应用程序有CORS标题。 – Tushar

相关问题