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

问题描述

我是 angular2/4 web api 的新手.我想使用 angular 4 web api http 将一些数据保存到数据库中.当我从 angular 调用 GET 方法时,它工作正常,但不适用于 POST 方法.我得到 415 Unsupported Media Type.All 时间,但是当我使用邮递员时,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 需要一个 array 但你发送了一个 string,这就是为什么你得到一个状态码 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 post 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:02