Angular2/4 post 方法无效

Angular2/4 post method not working

我是 angular2/ 4 web api 的新人。我想使用 angular 4 web api http 将一些数据保存到数据库中。当我从 angular 调用 GET 方法时,它工作正常,但不适用于 POST 方法。我当时收到 415 Unsupported Media Type.All,但是当我使用 postman 时,post 方法工作正常,因为我将内容类型更改为 application/json。当我使用警报按钮时,我得到 [object,Object],但在 angular2/4 中它不起作用...我看到很多问题都具有相同的问题,但它们对我不起作用。

这是我的service.ts文件

 CatchStockDetail(stock: any)
    : Observable<IStockdetails[]> {
    alert(stock);      

          let stockdetail = JSON.stringify([{ stock }]);
     let headers = new Headers({ 'Content-Type': 'application/json' });
     let options = new RequestOptions({ headers: headers });
    return this._http.post('http://localhost:1234/api/NewStockCount/' + 'Stock', stockdetail,
        { params: [{ stock }] })
        //  .map(res => res.json());
        //     .map(res => res.json().stock)
        .map((response: Response) => <IStockdetails[]>response.json())
        .catch(this.handleError);
}

这是我的component.ts文件

 this._enqService.CatchStockDetail(this.stock)
            .subscribe(value => {
                value.forEach(stock => {
                    this.stockdetail.push(this.stock)
                });
            },
                error => {
                    console.error(error);
                    this.statusMessage = "Problem with the service.Please try again after sometime";
                });

这是我的api代码

 [HttpPost]
    public void Stock([FromBody] List<spGetNewStockCountDetails_Result> jsonvalues)

    {
        foreach (spGetNewStockCountDetails_Result Datastock in jsonvalues)
        {
            spGetNewStockCountDetails_Result Stockobject = new spGetNewStockCountDetails_Result();
            Stockobject.schid = Datastock.schid;
            Stockobject.itemid = Datastock.itemid;
            Stockobject.itemcode = Datastock.itemcode;
            Stockobject.itemdescription = Datastock.itemdescription;
            Stockobject.packingtypeid = Datastock.packingtypeid;
            Stockobject.count = Datastock.count;

            enqentities.spGetNewStockCountDetails(Datastock.schid, Datastock.itemid, Datastock.itemcode, Datastock.itemdescription, Datastock.packingtypeid, Datastock.count);
        }

    }

    public class spGetNewStockCountDetails
    {

        public int schid { get; set; }
        // public int id { get; set; }
        public int itemid { get; set; }
        public string itemcode { get; set; }
        public string itemdescription { get; set; }
        public int packingtypeid { get; set; }
        public int count { get; set; }

    }

这是我的错误

"{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"没有 MediaTypeFormatter 可用于读取类型

的对象
CatchStockDetail(stock: any): Observable<IStockdetails[]> {   

    let stockdetail = JSON.stringify([{ stock }]);
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers, params: [{ stock }]  });

    return this._http.post('http://localhost:1234/api/NewStockCount/' + 'Stock', stockdetail, options)
    .map((response: Response) => <IStockdetails[]>response.json())
    .catch(this.handleError);
}

注意到您正在使用 @angular/http 中已弃用的 HTTP 版本。您应该考虑将其更改为使用 HttpClient 而不是 @angular/common/http 参见:https://angular.io/api/common/http/HttpClient

你的 api 期待一个 array 但你发送了一个 string ,这就是你得到状态代码的原因共 415

并删除参数,在您的 api 中甚至不需要它。

CatchStockDetail(stock: any)
    : Observable<IStockdetails[]> {
    alert(stock);      

     let stockdetail = stock;
     let headers = new Headers({ 'Content-Type': 'application/json' });
     let options = new RequestOptions({ headers: headers });
     return this._http.post('http://localhost:1234/api/NewStockCount/' + 'Stock', stockdetail, options)
        .map((response: Response) => <IStockdetails[]>response.json())
        .catch(this.handleError);
}

关于您遇到的错误,请查看您的 api,您甚至没有返回数据,这就是为什么在 map 上出现错误,删除 map 将暂时解决您的问题。

您必须更新您的组件

this._enqService.CatchStockDetail(this.stock)
            .subscribe(value => {
if(typeof value !== 'undefined' && value != null){
         value.forEach(stock => {
              this.stockdetail.push(this.stock)
         });
     }
},
error => {
         console.error(error);
         this.statusMessage = "Problem with the service.Please try again after sometime";
});

尝试使用以下代码:

 CatchStockDetail(stock: any): Observable<IStockdetails[]> {
    let body = JSON.stringify([stock]);
    let url = 'http://localhost:1234/api/NewStockCount/' + 'Stock';
    let head: Headers = new Headers();
    head.set('Content-Type', 'application/json');
    head.set('Accept', 'application/json');

    return this._http.post(url, body, head)
      .map((response: Response) => response.json())
      .catch(this.handleError);
  }