如何将Angular 2中的数据(Json对象)发布到Web API?

时间:2018-07-13 05:55:24

标签: angular

角度代码为::

  

app.component.ts


Json数据

    this.objsearch=[{ "Key": "FirstName", "value": this.firstName },
        { "Key": "MiddleName", "value": this.middleName },
        { "Key": "fatherName ", "value": this.fatherName },
        { "Key": "motherName", "value": this.motherName },
        { "Key": "_UId", "value": this._UId },
        { "Key": "Country", "value": this.ddlcountry },
        { "Key": "State", "value": this.ddlState },
        { "Key": "City", "value": this.ddlCity },
        ]

调用service.ts的方法

      this.ServiceObject.saveData(Json.Strigify(this.objsearch)).subscribe(response => (this._param) = response);
  

Service.ts

public_saveDataURL="http://localhost:53915/api/ComapnyAndProduct/DeleteItem";

方法::

saveData(objSearch: any) 
{
    return this._http.post(this._saveDataURL,objSearch)
   .map((response: Response) => response.json()); 
}
  

网络Api方法

[HttpPost]
public IHttpActionResult DeleteItem([FromUri]Mregistration objSearch)
{
return Ok();
}

1 个答案:

答案 0 :(得分:0)

您无需在JSON.strigify(this.objsearch)处使用saveData

this.ServiceObject.saveData(this.objsearch).subscribe(response => (this._param) = response);

此外,您需要从正文而不是从网址获取数据。

[HttpPost]
public IHttpActionResult DeleteItem([FromBody]Mregistration objSearch)
{
   return Ok();
}

您的对象必须具有这样的结构

this.objsearch = {
   firstName: this.firstName,
   middleName: this.middleName,
   fatherName: this.fatherName,
   motherName: this.matherName,
   _Uid: this._Uid,
   country: this.ddlcountry,
   state: this.ddlState,
   city: this.ddlCity
}