【问题标题】:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. Angular请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'http://localhost:4200'。角
【发布时间】:2017-09-06 17:00:48
【问题描述】:

我正在尝试连接到 API,但出现错误。

XMLHttpRequest 无法加载 https://valgresultat.no/api/。不 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。

我的 proxy.config.json

{
    "/api/": {
       "target":  {
         "host": "valgresultat.no",
         "protocol": "http:",
         "port": 80
       },
       "secure": false,
       "changeOrigin": true,
       "logLevel": "info"
     }
   }

我的 app.component.ts

import { Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  private apiUrl = '/api/';
  data: any = {};

  constructor(private http: Http) {
    console.log('Hello fellow user');
    this.getContacts();
    this.getData();
  }
  getData() {
    return this.http.get(this.apiUrl).map((res: Response) => res.json())
  }

  getContacts() {
    this.getData().subscribe(data => {
      console.log(data);
      this.data = data
    })
  }
}

【问题讨论】:

标签: angular


【解决方案1】:

安装 CORS chrome 扩展程序然后你就可以连接到你的 api 请在下面找到安装 CORS 插件的链接 https://chrome.google.com/webstore/detail/cors/dboaklophljenpcjkbbibpkbpbobnbld?hl=en

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    相关资源
    最近更新 更多