Angular2 / 4发布方法不起作用 [英] Angular2/4 post method not working

查看:98
本文介绍了Angular2 / 4发布方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angular2 / 4 Web API的新手。我想使用angular 4 Web API http将一些数据保存到数据库。当我从角度调用GET方法时,它工作正常,但无法在POST方法上工作。我一直在获取415不支持的媒体类型,但是当我使用邮递员时,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:此资源不支持请求实体的媒体类型'text / plain'。, ExceptionMessage:没有MediaTypeFormatter可用于读取类型为

"{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type

推荐答案

您的api需要一个数组,但是您发送的是字符串 ,这就是为什么您得到状态代码415

Your api is expecting an array but you send a string instead, that is why you get a status code of 415

并删除该参数的原因,甚至在您的api中也不需要。

And remove the param, it is not even required in your 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 会立即解决您的问题。

About the error you're encountering, look at your api, you're not even returning a data, which why on map you get an error, remove the map will solve your issue for now.

您必须更新组件

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

这篇关于Angular2 / 4发布方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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