如何以角度 7 发布原始 json 数据

穆斯塔法·巴格里

我想将用户名从 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));

}

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

穆斯塔法·巴格里

很久之后我发现

       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));

回答

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章