2
respostas

Erro de CORS

Boa noite ,

Estou tomando erro de cors

Failed to load http://localhost:3000/v1/fotos: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Como posso resolver esse problema?

Baixei o código da aula 4

Obrigado.

2 respostas

Olá Edvaldo,

Cara eu tive a mesma dúvida que você, e um professor me respondeu.

Aqui o link.

Boa noite Wilton Ribeiro!

Obrigado pelo retorno.

Porem não resolveu o problema, segue aqui como codifiquei a classe, porem sem sucesso na requisição.

import { Component, Inject, } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';


@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  apiRoot: string ='http://localhost:3000/v1/fotos';
  http: Http;
  headers: Headers;
  options: RequestOptions;

  fotos: Object[] = [];

  constructor(http: Http) {

    let url: string = this.apiRoot;
    this.http = http;
    this.headers = new Headers();
    this.headers.append('Access-Control-Allow-Origin', '*');
    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Access-Control-Allow-Headers', 'content-type');
    this.headers.append('Access-Control-Allow-Headers', 'X-Requested-With');

    this.options = new RequestOptions({ headers: this.headers });

    http
    .get(url, this.options)
    .subscribe(res =>  console.log(res));
  }
}

E como resultado continua esse:

Failed to load http://localhost:3000/v1/fotos: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

Fico muito grato pela atenção.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software