【发布时间】:2016-02-02 10:51:25
【问题描述】:
这是我在 angular2 项目中的服务,我想使用服务中的 create 方法来调用我的控制器。
create(maillingId: string, name: string, phone: string): Observable<void> {
var rp = new RecipientDto(maillingId, name, phone);
console.log(JSON.stringify(rp));
console.log(this.apiUrl);
console.log(this.headers);
return this.http.post(this.apiUrl, JSON.stringify(rp), { headers: this.headers })
.do(null,
() => {
})
.map<any>(response => { });
}
这是我打印出来的 这是我可行的邮递员电话 最后是我的控制器代码
[HttpPost]
public IActionResult Post([FromBody]RecipientDto recipientDto)
{
MailingList ml = _repository.Get<MailingList>(recipientDto.MaillingId);
if (ml != null)
{
var rp = new Recipient(Guid.NewGuid());
rp.IsActivated = true;
rp.Name = recipientDto.Name;
rp.PhoneNumber = recipientDto.Phone;
ml.Recipients.Add(rp);
return Ok();
}
return HttpNotFound();
}
有一点要提一下,我的前端create方法被调用的时候没有fiddler记录。
delete 的问题相同(也不要调用 api),但 getAll() 是可行的
getAll(id:string): Observable<Recipient[]> {
return this.http.get(`${this.apiUrl+'/'+id}`)
.map<any>(res => <any>res.json())
.map(a=> <Recipient[]>a);;
}
和我的 get 控制器
[HttpGet("{mlId}")]
public IEnumerable<Recipient> Get(string mlId)
{
List<Recipient> list = _repository.Get<MailingList>(mlId).Recipients;
return list;
}
【问题讨论】:
-
您是否尝试过从您的
post通话中删除.do(null, () => {})?我不确定你想用它来完成什么。 -
试过了,还是不行。 fiddler 没有出现任何记录。
-
你
subscribe到你的create方法了吗?我相信 Observables 在订阅之前不会真正触发。 -
嗨原来你必须订阅 post call 否则它不会调用 api
标签: typescript angular