简体   繁体   English

角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头

[英]Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm trying to get some data with Spotify / Musixmatch API in my Angular 4 app but it is not working. 我正在尝试在Angular 4应用程序中使用Spotify / Musixmatch API获取一些数据,但无法正常工作。 I keep getting this error: 我不断收到此错误:

XMLHttpRequest cannot load http://api.musixmatch.com/ws/1.1/album.get?album_id=14250417&apikey=xyz010xyz . XMLHttpRequest无法加载http://api.musixmatch.com/ws/1.1/album.get?album_id=14250417&apikey=xyz010xyz 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:9000 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:9000 '。

JS JS

  let headers = new Headers();
    headers.append('Content-Type','application/json');
    headers.append('Accept', 'application/json');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT');
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Headers', "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding");
    let options = new RequestOptions({ headers: headers });
    console.log(options)
    return this.http.get(this.url + 'album.get?album_id=' + album_id + '&apikey=' + this.apikey, options)
      .map(res => res.json())
  }

在此处输入图片说明

This is the problem with browser, typically its a security concern not to allow other requests which may lead to XSS attack easily. 这是浏览器的问题,通常是出于安全考虑,不允许其他容易导致XSS攻击的请求。 If only for development I suggest you to install a plugin which will disable in your browser plugin 如果仅用于开发,建议您安装一个将在浏览器插件中禁用的插件

If for production, then you need to configure your API. 如果用于生产,则需要配置API。

First, you have to create a file called proxy.conf.json, then put the following code { "/ restapiserver / *": { "target": "http: // localhost: 8075", "secure": false, "changeOrigin": true } } 首先,您必须创建一个名为proxy.conf.json的文件,然后输入以下代码{“ / restapiserver / *”:{“ target”:“ http:// localhost:8075”,“ secure”:false,“ changeOrigin“:true}}

In the service file, in the url of the rest service, delete the domain and leave the root of the service 在服务文件的其余服务的URL中,删除域并保留服务的根目录

private url: string = '/ restapiserver /'; 私人网址:string ='/ restapiserver /';

PS: In AngularJS 4 PS:在AngularJS 4中

Try this if not yet problem solved by setting header in js. 如果尚未通过在js中设置标头解决问题,请尝试此操作。

Add "no access control allow origin" plugin/add-on in chrome. 在Chrome中添加“禁止访问控制允许来源”插件/附加组件。

Yo can find here: NoAccessControlAllowOriginAddon 可以在这里找到: NoAccessControlAllowOriginAddon

暂无
暂无

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

相关问题 所请求的资源上不存在“ Access-Control-Allow-Origin”标头-Angular 5 - No 'Access-Control-Allow-Origin' header is present on the requested resource - Angular 5 Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource Access-Control-Allow-Origin header 存在于请求的资源上 - Access-Control-Allow-Origin header is present on the requested resource 请求的资源 .htaccess 上不存在 Access-Control-Allow-Origin 标头 - No Access-Control-Allow-Origin header is present on the requested resource .htaccess XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - Access-Control-Allow-Origin header is not present on the requested resource 请求的资源上不存在Access-Control-Allow-Origin标头 - No Access-Control-Allow-Origin header is present on the requested resource 请求的资源 nodejs 上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM