简体   繁体   English

如何从 Angular http 帖子中将 OKObjectResult 作为字符串返回

[英]How to return OKObjectResult as string from Angular http post

I'm struggling to find how to pass the payGateR string in my OkObjectResult to my component.我正在努力寻找如何将 OkObjectResult 中的 payGateR 字符串传递给我的组件。 Can you please point me in the right direction or explain to me what I'm doing wrong?您能否指出我正确的方向或向我解释我做错了什么? Currently, I am returning an object to the angular property of payGateResponse after converting it to string to avoid errors.目前,在将 object 转换为字符串以避免错误后,我将其返回给 payGateResponse 的 angular 属性。

I hope I have provided enough info.我希望我提供了足够的信息。 Any help is appreciated!任何帮助表示赞赏!

ASP.NET Web Api ASP.NET Web Api

    [HttpPost("PaymentDetails")] 
    public IActionResult PostPaymentDetails(PaymentDetails paymentDetails)
    {
        // Rest of Code

            if (status == "OK" || status == "AUTHORIZED")
            {
                return Ok(new
                { payGateR = decryptResponse });
            }
            else
            {
                return NotFound();
            }
    }

Angular service Angular服务

export class PaymentDetailsService {
 formData: PaymentDetails;
 readonly rootURL = 'https://localhost:44386/api/';

 constructor(private http: HttpClient) { }

 postPaymentDetails() {
   return this.http.post(this.rootURL + 'order/paymentdetails', this.formData);
 }
}

Angular Component Angular 组件

export class ShoppingCartFormComponent implements OnInit {
  order: IOrder[];
  payGate: IPayGate[];
  payGateResponse: string;

  cardBrands: string[] = [
    'VISA',
    'MasterCard'
  ];

  constructor(private http: HttpClient,
    private service: PaymentDetailsService, private toastr: ToastrService) { }

  ngOnInit(): void {
  }

  onSubmit(form: NgForm) {
    this.service.postPaymentDetails().subscribe(
      res => {
        this.resetForm();
        this.toastr.success('Payment Successful', 'Oh yeah!');
        this.payGateResponse = res.toString();
        console.log(res);
      },
      err => {
        this.toastr.error('Payment Not Successful');
        this.resetForm();
        console.log(err);
      }
    )
  }

Angular HttpClient default response body is JSON . Angular HttpClient默认响应正文是JSON If you want your response object to be something else you should add 'observe': 'body' to the options of your request.如果您希望您的响应 object 是其他内容,您应该在请求的选项中添加'observe': 'body'

So your request should look like this所以你的请求应该是这样的

 postPaymentDetails() {
   return this.http.post(this.rootURL + 'order/paymentdetails', this.formData, {observe: 'body'});
 }

Now you can put anything you want in the body of your response and you can access it via res.body in Angular现在您可以在响应正文中添加任何您想要的内容,并且可以通过res.body中的Angular访问它

Adding responseType: 'text' solved my problem.添加 responseType: 'text' 解决了我的问题。

Source: https://angular.io/guide/http#requesting-non-json-data来源: https://angular.io/guide/http#requesting-non-json-data

postPaymentDetails() {
    return this.http.post(this.rootURL + 'order/paymentdetails', this.formData, {                     
   responseType: 'text' });
}

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

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