Angular2 http post not passing the json object to MVC 6 controller action

久未见 提交于 2019-12-09 01:09:21
CEBE dev

I got a solution from this discussion and now I am able to post the Json data using angular2 Http request method instead of post.The controller action shows the object values while debugging. I have tested with the different types of objects and list of objects and everything is working fine.

Thanks @Pardeep Jain for your sample code.

BaseService.ts

protected ExecutePost(action: string, data: any) {
        let _body = JSON.stringify(data);
        let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });

        let requestoptions: RequestOptions = new RequestOptions({
            method: RequestMethod.Post,
            url: this._baseUrl + action,
            headers: headers,
            body: _body
        })

        return this.http.request(new Request(requestoptions))
            .map((res: Response) => { return res.json(); });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!