我想将用户名从 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] 删除。
我来说两句