【发布时间】:2020-05-02 22:53:23
【问题描述】:
我正在使用 web api 2 在 angular 4 中工作,我的 post call 不工作并没有点击 api。问题是它甚至没有给我一个错误。这是我的代码
registerUser() {
this.confirmPasswordMessage = false;
if (this.registerForm.valid) {
this.confirmPasswordMessage = false;
if (this._registerModel.Password.trim() !== this._registerModel.ConfirmPassword.trim()) {
this.confirmPasswordMessage = true;
this._progressBar.done();
return false;
}
const _data = JSON.stringify(this._registerModel);
this._apiService.postCall('Account/Register', _data).subscribe(resp => {
}, error => () => {
console.log(error); }, () => {
});
} else {
return false;
}
}
这是我的 api 服务代码。
public postCall<T>(apiUrl: any, itemName: any): Observable<T> {
const _options = { headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
}
)};
return this.http.post<T>(this.actionUrl + apiUrl, itemName, _options);
}
这是我传递的 json。当我通过邮递员运行这个 json 时,它命中了 api。但我不明白为什么它没有从角度应用程序中击中它。
"{"FirstName":"Kamran","LastName":"Khan","Email":"kamrankhan473@gmail.com","UserName":"kamrankhan","Password":"Kamran@786","ConfirmPassword":"Kamran@786"}"
我使用邮递员测试的 api 没有问题,它运行良好。
【问题讨论】:
-
如果它没有像您声称的那样命中 api(= 在 Network devtools 中也没有看到任何请求?),然后检查
registerUsermethod 是否被调用,如果确实如此 - 是表格实际上有效吗? (顺便说一句,你似乎也多余地重置了 this.confirmPasswordMessage 消息) -
1.不相关,您为什么要使用
JSON.stringify()将数据转换为字符串并提及'Content-Type': 'application/json'?数据应该在没有转换的情况下发送:this._apiService.postCall('Account/Register', this._registerModel). -
我认为你不需要 JSON.stringify 你的数据来进行 http.post() 调用
-
我也试过这个,不是对模型进行筛选,但仍然没有响应。
-
apiUrl 给了我这个链接,localhost:20863
标签: angular angular-http