【发布时间】:2021-03-16 13:21:13
【问题描述】:
这是我在 Visual Studio (Web API) 中生成 6 位 OTP 的地方
static int otp;
[HttpPost]
public void Post([FromBody] PhoneNum ph)
{
try
{
int otpValue = new Random().Next(100000, 999999);
otp = otpValue;
...
我正在使用 IHttpActionResult 返回 OTP 类对象
[Route("getotpsession")]
public IHttpActionResult GetOTPSession()
{
OTPClass otp_curr = new OTPClass();
otp_curr.current_otp = otp.ToString();
return Ok(otp_curr);
}
在 Angular 中,我尝试使用 subscribe() 获取此对象
getSessionOTP(){
this.session_otp = this.otpservice.getOTP().subscribe((data)=> {this.session_otp = data; console.log(this.session_otp)})
alert(this.session_otp)
}
问题是 -
- 我收到警报为 [object Object]
- console.log(this.session_otp) 给了我 {current_otp:"345262"} -- 这意味着正在获取对象
如何从返回的对象中获取 OTP? (如在 console.log 中) 我尝试了很多解决方案,其中大多数包括 JSON.stringify() 解决方案。没有一个工作。
请帮忙。
【问题讨论】:
-
R.理查兹,不,没有。但是,以下解决方案有效....无论如何,谢谢您的时间。 :)
标签: c# angular typescript visual-studio asp.net-web-api