Angular 跨域

Angular 跨域

https://github.com/angular/an...

假如待请求的接口服务器为 https://xxx.xxxxxxx.com/api/c...

项目根目录
proxy.config.json
target 为接口服务器

{
  "/api": {
    "target": "https://xxx.xxx.com/api",
    "changeOrigin": true,
    "secure": false,
    "logLevel": "debug",
    "pathRewrite": {
      "^/api": ""
    }
  }
}
export class AuthenticationService implements AuthService {
    API_URL = '/api/';
    API_ENDPOINT_LOGIN = 'card/Login';

    public login(credential: Credential): Observable<any> {
        // Expecting response from API
        // tslint:disable-next-line:max-line-length
        // {"id":1,"username":"admin","password":"demo","email":"[email protected]","accessToken":"access-token-0.022563452858263444","refreshToken":"access-token-0.9348573301432961","roles":["ADMIN"],"pic":"./assets/app/media/img/users/user4.jpg","fullname":"Mark Andre"}
        return this.http.get<AccessData>(this.API_URL + this.API_ENDPOINT_LOGIN + '?' + this.util.urlParam(credential)).pipe(
            map((result: any) => {
                if (result instanceof Array) {
                    return result.pop();
                }
                return result;
            }),
            tap(this.saveAccessData.bind(this)),
            catchError(this.handleError('login', []))
        );
    }
}

相关推荐