【问题标题】:Display soap response Angular显示肥皂响应角
【发布时间】:2020-09-27 22:45:58
【问题描述】:

如何在 Angular 客户端应用程序中显示 SOAP 响应?请求通过下面的 Nodejs 服务器发送。我在控制台中收到响应。我想在前端显示响应。如何在角度前端显示某些/所有字段?例如。我想显示响应中的交易编号。我将如何从响应中检索该值并将其显示在前端?

app.js

app.get("/api/transunion", (req, res) => {

var base64tag = CryptoJS.enc.Base64.stringify(hashtag);

var soapRequest = `<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="http://autoinsight.transunion.co.za/types">
    <soap:Header/>
        <soap:Body>
                <typ:GetConvergedDataRequest xmlns="http://autoinsight.transunion.co.za/types" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                  <typ:ApiKey>96C0C1D2-1278-4C26-AC52-78B287708AC3</typ:ApiKey>
                  <typ:ReportCode>A4B4AC82-2F2F-4A4B-AFF4-2BE2E35238D1</typ:ReportCode>
                  <typ:Input i:type="HPIReportRequest">
                    <typ:SubscriptionUsername></typ:SubscriptionUsername>
                    <typ:SubscriptionPassword></typ:SubscriptionPassword>
                    <typ:VehicleMMCode>46620030</typ:VehicleMMCode>
                    <typ:VehicleManufactureYear>2014</typ:VehicleManufactureYear>
                    <typ:ClientReference>BryteSA</typ:ClientReference>
                    <typ:RequestorPerson></typ:RequestorPerson>
                    <typ:GuideYear>2020</typ:GuideYear>
                    <typ:GuideMonth>4</typ:GuideMonth>
                    <typ:VehicleMileage>0</typ:VehicleMileage>
                  </typ:Input>
                </typ:GetConvergedDataRequest>
          </soap:Body>
      </soap:Envelope>`;

      request.post({
        uri: url,
        form: soapRequest,
        headers: {"request-hash": base64tag}
      }, function(err, response, body){
        console.log(body);
        console.log(response.statusCode);
        parseString(body, function(err, result){
          if(err){
            console.log("ERROR: " + err);
          }
          console.log(result);
          res.send(result);
        });
      });});

客户端

component.ts 文件

import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import * as converter from 'xml-js';

@Component({
  selector: 'app-vehicle-details',
  templateUrl: './vehicle-details.component.html',
  styleUrls: ['./vehicle-details.component.css']
})
export class VehicleDetailsComponent implements OnInit {

  data: any;
  TransactionNumber: any;
  EstimatedRetailPrice: any;

  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.http.get('http://localhost:3000/api/tu', {responseType: 'text' })
        .subscribe((resData: any) => {
          let result1 = converter.xml2json(resData.toString(), {compact: true, spaces: 4});
          const JSONData = JSON.parse(result1);
          this.data = JSONData;
          this.TransactionNumber = JSONData.TransactionNumber;
          this.EstimatedRetailPrice = JSONData.EstimatedRetailPrice;
          console.log(this.TransactionNumber);
        });
  }
}

JSON 响应

  {
       "_declaration":{
          "_attributes":{
             "version":"1.0",
             "encoding":"utf-8"
          }
       },
       "s:Envelope":{
          "_attributes":{
             "xmlns:s":"http://www.w3.org/2003/05/soap-envelope"
          },
          "s:Body":{
             "GetConvergedDataRequestResponse":{
                "_attributes":{
                   "xmlns:i":"http://www.w3.org/2001/XMLSchema-instance",
                   "xmlns":"http://autoinsight.transunion.co.za/types"
                },
                "ConvergedData":{
                   "_attributes":{
                      "xmlns:d4p1":"http://schemas.datacontract.org/2004/07/Transunion.Auto.Convergence.B2B.BusinessModels",
                      "i:type":"d4p1:ConvergedResults"
                   },
                   "d4p1:AccidentHistory":{
                      "_attributes":{
                         "i:nil":"true"
                      }
                   },
                   "d4p1:TransactionNumber":{
                      "_text":"64326668"
                   },
                   "d4p1:ValidationMessages":{
                      "_attributes":{
                         "xmlns:d5p1":"http://schemas.microsoft.com/2003/10/Serialization/Arrays"
                      }
                   },
                   "d4p1:VehicleCodeAndDescptionInfo":{
                      "d4p1:VehicleCodeAndDescription":{
                         "d4p1:ResultCode":{
                            "_attributes":{
                               "i:nil":"true"
                            }
                         },
                         "d4p1:ResultCodeDescription":{
                            "_attributes":{
                               "i:nil":"true"
                            }
                         },
                         "d4p1:VehicleCode":{
                            "_text":"46620030"
                         },
                         "d4p1:VehicleTypeCode":{
                            "_text":"T"
                         },
                         "d4p1:VehicleTypeDescription":{
                            "_text":"Agricultural"
                         },
                         "d4p1:VehicleMake":{
                            "_text":"NEW HOLLAND"
                         },
                         "d4p1:VehicleModel":{
                            "_text":"TT TN"
                         },
                         "d4p1:VehicleVariant":{
                            "_text":"TT 55 E / T DT"
                         },
                         "d4p1:IntroductionDate":{
                            "_text":"2010-03-15T00:00:00"
                         }
                },
                "ReportUrl":{
                   "_text":"https:\\\\autotest.transunion.co.za/ReportApi/apireport/270c70e5-7d6d-407f-adc1-71974faec7df"
                },
                "ResponseStatus":{
                   "_attributes":{
                      "xmlns:d4p1":"http://schemas.servicestack.net/types",
                      "i:nil":"true"
                   }
                }
             }
          }
       }
    }

【问题讨论】:

    标签: node.js json angular soap response


    【解决方案1】:

    您需要通过 Angular 绑定将响应的字段绑定到组件成员。

    在您的 Angular 组件中,例如:

    • 创建一个新成员:

    订阅用户名:字符串成员

    • 在您的 subscribe() 中使用 SOAP NPM 库来解析 SOAP 响应,然后:

    this.subscriptionUsername = soapData.subscriptionUsername

    • 在你看来,那么你可以:

      收到 {{ 订阅用户名 |异步 }}

    【讨论】:

    • 响应转换成json后如何输出特定字段?
    • 恐怕我无法弄清楚您的问题是什么。在我之前的示例中,您已经显示了特定字段订阅用户名。 subscriptionUSername 不是整个 JSON,而只是特定部分。请您更准确地描述您想要实现的目标吗?
    • 我的问题是,由于响应中的命名空间,我无法访问特定字段,然后输出。
    • @FayyaadhKarolia,你要输出的“特定字段”是什么?
    • 我想输出响应中的每个字段,例如 TransactionNumber
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多