Error CORS api Post Office

Asked

Viewed 1,747 times

2

I am trying to make a request for freight calculation for the post office, but when I request the request, returns the error error of CORS --

Error message:

Access to XMLHttpRequest at 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo?nCdEmpresa=&sDsSenha=&nCdServico=4510&sCepOrigem=72001835&sCepDestino=75180000&nVlPeso=1&nCdFormato=1&nVlComprimento=16&nVlAltura=5&nVlLargura=15&nVlDiametro=0&sCdMaoPropria=n&nVlValorDeclarado=100&sCdAvisoRecebimento=n' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Call code:


  testeCorreios(){
    let url = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo';

    //let h = new HttpHeaders()
    //.append('Access-Control-Allow-Credentials', 'true')
    //.append('Access-Control-Allow-Methods', '*')
    //.append('Access-Control-Allow-Origin', '*');

    let p = new HttpParams()
    .set('nCdEmpresa', '')
    .set('sDsSenha', '')
    .set('nCdServico', '4510')
    .set('sCepOrigem', '72001835')
    .set('sCepDestino', '75180000')
    .set('nVlPeso', '1')
    .set('nCdFormato', '1')
    .set('nVlComprimento', '16')
    .set('nVlAltura', '5')
    .set('nVlLargura', '15')
    .set('nVlDiametro', '0')
    .set('sCdMaoPropria', 'n')
    .set('nVlValorDeclarado', '100')
    .set('sCdAvisoRecebimento', 'n');

    return this.http.get(url, {
      headers: h,
      responseType: 'text',
      params: p
    });

  }

I tried to add a header and stuff, but it didn’t work. For testing I was using a Chrome plugin to pass, but when I go to production it does not work (until pq is not with the plugin). Somebody help me out there

2 answers

0

I had the same problem with the ZIP code consultation, I decided doing it this way, it might help you there:

export class ConsultaCepService {

  constructor(private backend: HttpXhrBackend) { }

  consultaCEP(cep: string): Promise<any> {
    /** Nova variável "cep" somente com dígitos. */
    cep = cep.replace(/\D/g, '');
    /** Expressão regular para validar o CEP */
    const validacep = /^[0-9]{8}$/;
    /** Valida o formato do CEP. */
    if (validacep.test(cep)) {
      const request = new HttpRequest('GET', `//viacep.com.br/ws/${cep}/json/`);

      const events$: Observable<HttpEvent<any>> =
        of(request).pipe(concatMap((req: HttpRequest<any>) => this.backend.handle(req)));

      // Filtra apenas o evento de resposta
      const res$: Observable<HttpResponse<any>> = events$.pipe(
        filter((event: HttpEvent<any>) => event instanceof HttpResponse)) as Observable<HttpResponse<any>>;

      return res$.pipe(map((res: HttpResponse<any>) => res.body)).toPromise();
    } else {
      return Promise.reject(cep);
    }
  }
}

  • It did not work friend, still gives the error of Cors. But thanks for the attempt. I think what needs to be done is to add something in the header to give access and such

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.