【发布时间】: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