【问题标题】:unable make http post request in angular4 with backend server无法使用后端服务器在 angular4 中发出 http post 请求
【发布时间】:2017-07-11 09:24:29
【问题描述】:

我在 angualr4 http post request 中有一个奇怪的问题,即当我使用完整的后端服务器 api URL 'http://localhost:9090/api/login' post request 时没有发出。但是当我简单地使用'/api/login'时。使用“http://localhost:4200/api/login”发布请求。下面是我的代码

authentication.service.ts

认证(模型){

    console.log('this.globalConstant.apiLoginURL',this.globalConstant.apiLoginURL);

    console.log('this.apiLoginURL',this.apiLoginURL);


    let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
    let options       = new RequestOptions({ headers: headers }); // Create a request option

    let bodyString = JSON.stringify(model); // Stringify payload    


    console.log('headers==>'+headers);
    console.log('options==>'+options);
    console.log('bodyString==>'+bodyString);


    return this.http.post(this.endpoint+'/api/login', bodyString, options)
               .map(this.extractData)
               .catch(this.handleError);        


}

login.component.ts

login() {      
    this.newsDataService.authenticate(this.account)
        .subscribe(
            data => {
                this.router.navigate([this.returnUrl]);
            },
            error => {
                this.alertService.error(error);
                console.log('alertService==>'+error);                   
            },
            () => console.log("Finished") 

            );
}

【问题讨论】:

  • 可以给我们console.log(this.endpoint);的日志吗?
  • 可能您遇到了 CORS 问题,因为您的应用和端点位于不同的端口上!
  • localhost:9090。我没有看到在网络选项卡中发出任何请求
  • 我告诉过你,如果你没有在“网络”选项卡中看到请求,那么你就没有调用它。但也许更多的代码会帮助我们理解!

标签: angular http post http-post


【解决方案1】:

一定是CROS问题。检查您的控制台日志以了解详细信息。

如果您使用的是 Chrome,请尝试在 chrome 快捷方式中添加 --disable-web-security。

Disble Web Security

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多