【发布时间】:2017-08-16 07:33:23
【问题描述】:
我有从 EmpID 获取数据的 Web API 方法,我从 Angular 2 调用此方法并希望将数据绑定到表单以更新它。
我认为我没有从服务中正确调用 Web API 方法, 但它给出了我们找不到这个 Web API 方法的错误。
Web API 方法:
[ Route("api/Employee/GetEdit/{id:int}") ]
public Employee GetEdit(int id) {
return db.Employees.Where(t => t.EmpID == id).FirstOrDefault();
}
以下是我的服务:
GetEdit(id:any) {
let headers = new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.get('http://localhost:49221/Employee/GetEdit/' + id, headers)
.map((res: Response) => res.json());
//return this.http.get('http://localhost:49221/api/Employee')
// .map(this.extractData)
// .catch(this.handleError);
}
下面是我的组件类:
getEdit() {
this._service.GetEdit(1).subscribe(
posts => this.employee = posts,
error => console.error(error)
)};
【问题讨论】:
-
如果您收到跨源请求错误,请检查浏览器中的网络选项卡
-
@Malwaregeek ,是的,我遇到了跨源网络错误。
标签: angular asp.net-web-api asp.net-web-api2