为什么http.post没有在angular 2中调用我的控制器 [英] why http.post didn't call my controller in angular 2

查看:92
本文介绍了为什么http.post没有在angular 2中调用我的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在angular2项目中的服务,我想在服务中使用create方法来调用我的控制器.

this is my service in my angular2 project and I would like to use the create method in the service to call my controller.

 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 => { });

}

这是我打印出的内容 这是我可行的邮递员电话 最后是我的控制器代码

this is what I print out this is my workable postman call and last is my controller code

 [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方法被调用时,没有提琴手的记录.

one thing to mention that there is no fiddler record when my front-end create method is called.

相同的删除问题(也不要调用api),但对于getAll()可行

same question for delete(also don't call the api) but for the getAll() is workable

 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;
    }

推荐答案

我在这里有一个工作中的帖子示例:

I have a working post sample here:

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);

好像您缺少订阅部分.

此处有更多信息: > http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http

这篇关于为什么http.post没有在angular 2中调用我的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆