Angular 6中的Post方法不发送数据

时间:2018-08-10 10:12:50

标签: c# api http-post angular6

当我用get进行api调用时,当我转换为发布参数给我null值时,它可以完美工作。我必须使用[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
        }

2 个答案:

答案 0 :(得分:1)

我有类似的代码,可以在角度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();

        }

答案 1 :(得分:0)

要触发http.post方法,您必须在某个地方订阅它。参见角度API https://angular.io/guide/http#making-a-delete-request

“始终订阅!直到您在该方法返回的可观察对象上调用subscribe()时,HttpClient方法才会开始其HTTP请求。这对于所有HttpClient方法都是如此。”