如何在angular 7中发布原始json数据

问题描述 投票:0回答:1

我想将用户名从angular发布到asp.net Web api。我从服务器发布的方法是:

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

和斜角

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

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

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 test

json angular http-post
1个回答
0
投票

以这种格式发送数据:

{userName:userName}
© www.soinside.com 2019 - 2024. All rights reserved.