Bob*_*ang 2 typescript angular
这是我在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 => { });
}
Run Code Online (Sandbox Code Playgroud)
这是我打印出来的
这是我可行的邮递员电话
最后是我的控制器代码
[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();
}
Run Code Online (Sandbox Code Playgroud)
有一点需要注意的是,当我的前端创建方法被调用时,没有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);;
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器获取
[HttpGet("{mlId}")]
public IEnumerable<Recipient> Get(string mlId)
{
List<Recipient> list = _repository.Get<MailingList>(mlId).Recipients;
return list;
}
Run Code Online (Sandbox Code Playgroud)
我在这里有一个工作的帖子样本:
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://some-url/',
JSON.stringify({firstName:'Joe',lastName:'Smith'}),
{headers:headers})
.map((res: Response) => res.json())
.subscribe((res:Person) => this.postResponse = res);
Run Code Online (Sandbox Code Playgroud)
看起来你错过了订阅部分.
这里有更多信息:http: //www.syntaxsuccess.com/viewarticle/angular-2.0-and-http
| 归档时间: |
|
| 查看次数: |
1865 次 |
| 最近记录: |