ANGULAR4 Resolving cross-domain issues when using httpclient

Source: Internet
Author: User
Tags export class

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.