简体   繁体   English

如何从角度的API调用接收字符串作为返回?

[英]How to receive a string as a return from an API call in angular?

I need to get response from my API call in form of string. 我需要以字符串的形式从我的API调用中获得响应。 My API function is simple (for testing) 我的API函数很简单(用于测试)


        [HttpPost("verifyCode")]
        public ContentResult verifyCode([FromBody] UserM resetModel)
        {
            return "This is response";
        }

API Call API调用

verifyCode(username, code){
      const body = 'username=' + username + '&code=' + code;
       let test = new UserM();
       test.username = username;
       test.code = code;
       const requestHeaders: HttpHeaders = new HttpHeaders({
        'Content-Type': 'application/json;charset=utf-8'
      });
      return this.http.post<string>('http://localhost:12763/api/account/verifyCode/', test, {headers: requestHeaders, withCredentials: true});
    }

i am trying to get the output here 我想在这里得到输出


 this.userService.verifyCode(username,code).subscribe(
        (data)=>{
          console.log(data);
        }
      )

But i dont get the output on console but get this error 但我没有得到控制台上的输出,但得到这个错误

SyntaxError: Unexpected token R in JSON at position 0 SyntaxError:位于0的JSON中的意外标记R.

Change your backend response and try it will be work 更改后端响应并尝试它将起作用

[HttpPost("verifyCode")]
public ContentResult verifyCode([FromBody] UserM resetModel)
{
    return ContentResult ("This is response","text/plain",Encoding.UTF8);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM