【发布时间】:2020-11-10 07:46:17
【问题描述】:
我是 Angular 的新手。有人可以帮我知道如何用角码调用我的 api 吗?
我的 We API 是 -
[HttpGet,Route("api/customer/getCustomer/{name}")]
public async Task<IHttpActionResult> getCustomer([FromUri] string name) {
...}
并且在 anular 中,我想像下面这样调用 -
customer.component.ts -
name = "test";
getCustomer(){
this.custService.getCustomer(this.name).subscribe((data) => {
this.cust = JSON.parse(data.toString());
});
}
custService 代码 -
getCustomer(name){
return this.http.get('https://localhost:44400/api/customer/getCustomer/', +name)
}
【问题讨论】:
-
get调用中的逗号需要删除:this.http.get('https://localhost:44400/api/customer/getCustomer/' + name)。此外,如果后端已经在发送有效 JSON 中的对象,则不需要JSON.parse和toString。你可以直接this.cust = data.
标签: c# angular asp.net-web-api