简体   繁体   中英

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. My API function is simple (for testing)


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

API Call

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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