-5

localhost/:1 Access to XMLHttpRequest at 'http://185.50.185.18:8080/api/cliente' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 import { Injectable } from '@angular/core';
 import {HttpClient, HttpHeaders} from '@angular/common/http';
 import {Observable} from 'rxjs/Observable';
 @Injectable({
    providedIn: 'root'
})
  export class PersonaService {

 constructor(private httpClient: HttpClient) { }

 obtenerTodasLasPersonas():Observable<any>{
  return this.httpClient.get("http://185.50.185.18:8080/api/cliente");

 }

agregarPersona(persona: any){
  let json = JSON.stringify(persona);
  let headers = new HttpHeaders().set('Content-Type', 
  'application/json',);

  return this.httpClient.post("http://185.50.185.18:8080/api/cliente", 
  json, {headers: headers});
}

eliminarPersona(identificador): Observable<any>{
  return this.httpClient.delete("http://185.50.185.18:8080/api/cliente" + 
identificador);
}

 }
  • Possible duplicates of: [Why does my JavaScript get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when Postman does not?](https://stackoverflow.com/questions/20035101/), [How to solve 'Redirect has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header'?](https://stackoverflow.com/questions/46522749/) – Igor Apr 23 '19 at 19:31
  • This is a very common question, at least under the Angular tag. Also, just throwing up some code and the error you received is not generally well received here. More background would be nice, and for this question necessary. If you dont tell us your back end we cant tell you how to configure CORS – TheBatman Apr 23 '19 at 19:53
  • Another day, another 20 posts about CORS errors. – R. Richards Apr 23 '19 at 20:23

2 Answers2

0

[EN-US] CORS will block requests that aren't being made from the same address on the same port. If you have access to the api running on http://185.50.185.18:8080/api/cliente you need to change it's configuration to enable CORS (each framework has it's own way of doing this, so I won be able to tell you how to do it in your case).

0

You are using nodejs as your server side to allow cors and avoid cors block use node cors package In your main server file add cors middleware

var cors = require('cors')

app.use(cors())

For details npm-cors-middleware

Muhammad Shareyar
  • 772
  • 2
  • 7
  • 21