首页 > 解决方案 > Angular 6中的发布方法不发送数据

问题描述

当我使用 get 进行 api 调用时,当我转换为发布它在参数中给我空值时,它工作得很好。我必须使用 [FromBody] 然后只调用 api 但仍然给我参数值 null。我也处理过CORS。

 const httpOptions = {
  headers: new HttpHeaders({      
    'Content':"application/json",
    'Content-Type':  'application/json;charset=utf-8',
    //'Content-Type':  'application/x-www-form-urlencoded; charset=UTF-8',
    'Authorization': 'Basic ' + btoa(Username + ":" + Password),        
  }),         
};

 checkLogin(form){        
    var obj = { Business_Email: form["Business_Email"], Business_Password: form["Business_Password"] };        
    return this.http.post(this.API_URL+"/Login/LoginValid", JSON.stringify(obj),httpOptions)


  }

我在 Asp .net C# 中有 Web Api

    [HttpPost]
        [ActionName("LoginValid")]
        [Route("api/Login/LoginValid")]
        public ReturnObject LoginValid([FromBody]string businessMaster)
        {
           //Code
        }

标签: c#apihttp-postangular6

解决方案


我有类似的代码可以在 angular 6 上正常工作。

addEmployee(emp: Employee): Observable<number> {
   return this.http.post(this._baseURL + "/api/employee", emp).pipe(
    map(success => success.status))
   }



[HttpPost]
        public void Post([FromBody]Employee employee)
        {
            db.Employees.Add(employee);
            db.SaveChanges();

        }

推荐阅读