简体   繁体   English

如何在Angular2中使用Backend作为Liferay服务器启用cors问题

[英]How to enable cors issue in Angular2 with Backend as Liferay server

I am new to Angular2. 我是Angular2的新手。 I have to create new Angular application and Need to use services from Liferay server. 我必须创建新的Angular应用程序并且需要使用Liferay服务器中的服务。

But When I call the Service using below code, it is giving Cors error. 但是当我使用下面的代码调用服务时,它会给Cors错误。

var headers = new Headers();

    headers.append('Access-Control-Allow-Origin', '*');
    headers.append("Content-Type", "application/x-www-form-urlencoded");
    let params: URLSearchParams = new URLSearchParams();   

    this.http.get(localStorage.getItem('domainName')+'/service-name', {headers: headers})
        .subscribe(response => {
            // login successful if there's a jwt token in the response
            console.log('Response'+response.json());
    });

My backend method is GET and I have added Cors filter property in my /webapps/Root/web.xml file 我的后端方法是GET,我在/webapps/Root/web.xml文件中添加了Cors过滤器属性

BUt in browser console it is giving below error: 在浏览器控制台中,它给出了以下错误:

XMLHttpRequest cannot load http://localhost:9080/service-name . XMLHttpRequest无法加载http:// localhost:9080 / service-name Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:4200 ' is therefore not allowed access. 因此不允许来源' http:// localhost:4200 '访问。 The response had HTTP status code 401. 响应具有HTTP状态代码401。

This error comes up when the backend's response does not have the necessary Access-Control-Allow-Origin header. 当后端的响应没有必要的Access-Control-Allow-Origin标头时,会出现此错误。 Please double check that the backend's headers are properly configured. 请仔细检查后端的标头是否已正确配置。 Also, the frontend's request does not have to have the Access-Control-Allow-Origin header ever. 此外,前端的请求不必具有Access-Control-Allow-Origin标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM