Environment for cross-domain issues:
When developing locally, use the command line NG s to turn on the service and access the address http://localhost:4200
Assume that the service address of the calling data interface is Http://localhost:8088/api/data.action
Ways to troubleshoot cross-domain issues:
Under the ANGULAR4 project root directory, create a file Proxy.config.json with the following file contents:
{ "/api": { "target": "http://localhost:8088" }}
Then using the command line ng s--proxy-config proxy.config.json re-open the service, the interface service proxy, you can use
Http://localhost:4200/api/data.action to request the interface service, then the problem is solved, because now does not cross the domain.
Many people on the Internet ask why this method or can not solve, that is because the way to open the service is not correct, to add parameters, pay attention to my red place above.
Use HttpClient to request the relevant code for the data:
Hero.service.ts
Import {injectable} from ' @angular/core 'rxjs' @angular/common/http '. /data/hero '; @Injectable ({ ' root '}) export class Heroservice { Constructor (private http: HttpClient) {} getheroes (): Observable<Hero[]> { returnthis.http.get <Hero[]> ("/api/data.action");} }
The code I gave is not complete, it's not important, just looking at the red part is enough, this example is based on the official example.
ANGULAR4 Resolving cross-domain issues when using httpclient