如何 post 原始 json 数据 angular 7

how to post raw json data in angular 7

我想 post 用户名从 angular 到 asp.net web api。 我来自服务器的 post 方法是:

 [Route("CheckUserName")]
    [AllowAnonymous]
    [HttpPost]
    public async Task<IHttpActionResult> CheckUsernameExist([FromBody] string username){ //return true or false}

并在 angular

checkUserName(userName: string) {
return this.http.post(
  `${this.appConfig.apiEndpoint}/${CHECK_USER_NAME}`,{userName})
  .pipe(map(res => res['result']['status'] as boolean));}

{userName} 是发送到服务器的数据 但服务器端收到空 我也测试数据:

userName,
{username:userName},
'"'+userName+'"',
,...

  checkUserName(userName: string) {
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post(
  `${this.appConfig.apiEndpoint}/${CHECK_USER_NAME}`, { userName },{ headers: headers })
  .pipe(map(res => res['result']['status'] as boolean));

}

我也用 postman 测试并正确获取服务器端的数据

以这种格式发送您的数据:

{userName:userName}

找了很久才发现

       let reqHeaders = new HttpHeaders().set('Content-Type', 'application/json');
   return this.http.post(`${this.appConfig.apiEndpoint}/${CHECK_USER_NAME}`, JSON.stringify(userName), { headers: reqHeaders })
      .pipe(map(res => res['result']['status'] as boolean));