这是我在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();
}
有一点需要注意的是,当调用我的前端创建方法时,没有fiddler记录。
同样的删除问题(也不要调用api),但getAll()是可行的
getAll(id:string): Observable<Recipient[]> {
return this.http.get(`${this.apiUrl+'/'+id}`)
.map<any>(res => <any>res.json())
.map(a=> <Recipient[]>a);;
}
和我的控制器获取
[HttpGet("{mlId}")]
public IEnumerable<Recipient> Get(string mlId)
{
List<Recipient> list = _repository.Get<MailingList>(mlId).Recipients;
return list;
}
答案 0 :(得分:6)
我在这里有一个工作岗位样本:
override func viewDidLoad() {
super.viewDidLoad()
self.getAPI1()
self.getAPI2()
self.getAPI3()
}
func getAPI1() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI1()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}
func getAPI2() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI2()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}
func getAPI3() {
do {
// try ... Get API Process
} catch {
let alertView = UIAlertView(title: "ERROR", message: "There is an error on getAPI3()", delegate: nil, cancelButtonTitle: "OK")
alertView.show()
}
}
看起来你错过了订阅部分。
这里有更多信息: http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http