Angular2 HTTP Post ASP.NET MVC Web API

前端 未结 7 1824
栀梦
栀梦 2020-12-06 11:23

How do you properly create a Web API POST of complex object or multiple parameters using Angular2?

I have a service component in Angular2 as seen below:



        
7条回答
  •  执笔经年
    2020-12-06 11:49

    If you call Web API 2.2 post method from Angular 2 type script, dont forget to add following header content and parameter object.

    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }); 
     var params = new URLSearchParams();
            params.set('userid', '102');
            params.set('username', 'foo');
    
     return this._http.post('http://localhost:6579/api/PostUser', params.toString(), { headers: headers }).map(res => res.json());
    

提交回复
热议问题