CORS api MAIL ERROR
I am trying to make a request for freight calculation to the post office, but when I request the request, it 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've tried adding a header and such, but it didn't work. For testing I was using a Chrome plugin to pass, but when I go up to production it does not work (even because it does not work with the plugin). Someone help me aii
2
2 answers
I had the same problem with the ZIP code query, I solved doing this way, it may 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);
}
}
}
0
Author: i9on i9on, 2020-03-06 13:55:53
Well, I solved by adding this url to the start of my url
-> https://cors-anywhere.herokuapp.com /
Then my call url looked like this:
0
Author: mGois, 2020-03-10 18:38:46