Error: That is not equal to the supplied origin

1

I'm trying to fetch records that are in an API on the heroku server with the following URL https://mdw-arm-wladimir.herokuapp.com/noticias

But when I receive this url in my Angular project it generates this error;

This is my Angular service class

import { Noticia } from './../core/model';
import { Injectable } from '@angular/core';

import { Http, Headers, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import * as moment from 'moment';

export class NoticiaFiltro {
  titulo: string;
  font: string;
  pagina = 0;
 itensPorPagina = 8;

}


@Injectable()
export class NoticiaService {


 noticiasUrl = 'https://mdw-arm-wladimir.herokuapp.com/noticias';


  constructor(private http: Http) { }

  pesquisar(filtro: NoticiaFiltro): Promise<any> {

    const params = new URLSearchParams();

    const headers = new Headers();

    params.set('page', filtro.pagina.toString());
    params.set('size', filtro.itensPorPagina.toString());

    if (filtro.titulo) {
      params.set('titulo', filtro.titulo);
    }

    if (filtro.font) {
      params.set('font', filtro.font);
    }

    return this.http.get('${this.noticiasUrl}', { headers, search: params  })
      .toPromise()
      .then(response => {
        const responseJson = response.json();
        const noticias = responseJson.content;

        const resultado = {
          noticias,
          total: responseJson.totalElements
        };

        return resultado;
    });
  }


     adicionar(noticia: Noticia): Promise<Noticia> {
            const headers = new Headers();
            headers.append('Content-Type', 'application/json');

            return this.http.post(this.noticiasUrl,
                JSON.stringify(noticia), { headers })
              .toPromise()
              .then(response => response.json());
        }

        excluir(codigo: number): Promise<void> {
          const headers = new Headers();

          return this.http.delete('${this.noticiasUrl}/${codigo}', { headers })
            .toPromise()
            .then(() => null);
      }


      atualizar(noticia: Noticia): Promise<Noticia> {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');

        return this.http.put('${this.noticiasUrl}/${noticia.codigo}',
            JSON.stringify(noticia), { headers })
          .toPromise()
          .then(response => {
            const lancamentoAlterado = response.json() as Noticia;

            this.converterStringsParaDatas([lancamentoAlterado]);

            return lancamentoAlterado;
          });
      }



      buscarPorCodigo(codigo: number): Promise<Noticia> {
        const headers = new Headers();

        return this.http.get('${this.noticiasUrl}/${codigo}', { headers })
          .toPromise()
          .then(response => {
            const noticia = response.json() as Noticia;

            this.converterStringsParaDatas([noticia]);

            return noticia;
          });
    }


        private converterStringsParaDatas(noticias: Noticia[]) {
          for (const noticia of noticias) {
            noticia.dataNoticia = moment(noticia.dataNoticia,
              'YYYY-MM-DD').toDate();
              }
            }



}

Would anyone know how to help me solve this problem?

    
asked by anonymous 09.03.2018 / 16:18

1 answer

2

According to the error, the domain that is making the request is not allowed to do so.

  

localhost: 4200

The browser validates this information through the Access-Control-Allow-Origin header sent in the request response. You can use the console (Network tab) of Chrome to check the headers sent by the Heroku server.

Using Postman, you can see that the authorized domain is http://localhost:8000 :

As the origin of the localhost:4200 request is different from the allowed localhost:8000 source by the Heroku server, the browser blocks the GET Request.

By default, Angle starts a server on port 4200. To resolve the problem, change the port the moment it starts serving:

ng serve --port 8000
    
09.03.2018 / 16:58