首页 > 解决方案 > node-soap 结果是深层嵌套对象而不是普通结果

问题描述

我使用 node-soap 来请求 SOAP-API。但结果并不如我所料。如果我使用 PHP 请求相同的内容(所有 api 示例都在 php 中),它会按预期工作。但是我的应用程序/服务是用 JavaScript (ExpressJS) 编写的。

我的请求代码很简单:

var url = '...wsdl';
var args = {
    Params: JSON.stringify({
      User: '...',
      AuthType: 'session',
      AuthData: '...',
      RequestType: 'get_mailaccounts',
      RequestParams: []
    })
};
soap.createClient(url, function(err, client) {
    client.Api(args, function(err, result, rawResponse, soapHeader, rawRequest) {
      console.log(util.inspect(result, {showHidden: false, depth: null}));
    });
});

结果如下所示:

{
  "return": {
    "attributes": {
      "xsi:type": "ns2:Map"
    },
    "item": [
      {
        "key": {
          "$value": "Request",
          "attributes": {
            "xsi:type": "xsd:string"
          }
        },
        "value": {
          "attributes": {
            "xsi:type": "ns2:Map"
          },
          "item": [
            {
              "key": {
                "$value": "RequestTime",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "$value": "1540823916",
                "attributes": {
                  "xsi:type": "xsd:int"
                }
              }
            },
            {
              "key": {
                "$value": "RequestType",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "$value": "get_mailaccounts",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              }
            },
            {
              "key": {
                "$value": "RequestParams",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "attributes": {
                  "SOAP-ENC:arrayType": "xsd:ur-type[0]",
                  "xsi:type": "SOAP-ENC:Array"
                }
              }
            }
          ]
        }
      },
      {
        "key": {
          "$value": "Response",
          "attributes": {
            "xsi:type": "xsd:string"
          }
        },
        "value": {
          "attributes": {
            "xsi:type": "ns2:Map"
          },
          "item": [
            {
              "key": {
                "$value": "FloodDelay",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "$value": "0.5",
                "attributes": {
                  "xsi:type": "xsd:float"
                }
              }
            },
            {
              "key": {
                "$value": "ReturnString",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "$value": "TRUE",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              }
            },
            {
              "key": {
                "$value": "ReturnInfo",
                "attributes": {
                  "xsi:type": "xsd:string"
                }
              },
              "value": {
                "attributes": {
                  "SOAP-ENC:arrayType": "ns2:Map[1]",
                  "xsi:type": "SOAP-ENC:Array"
                },
                "item": {
                  "attributes": {
                    "xsi:type": "ns2:Map"
                  },
                  "item": [
                    {
                      "key": {
                        "$value": "mail_login",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      },
                      "value": {
                        "$value": "xxxxxxx",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      }
                    },
                    {
                      "key": {
                        "$value": "mail_password",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      },
                      "value": {
                        "$value": "xxxxxxx",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      }
                    },
                    {
                      "key": {
                        "$value": "mail_adresses",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      },
                      "value": {
                        "$value": "xx@xx.de",
                        "attributes": {
                          "xsi:type": "xsd:string"
                        }
                      }
                    },
                  ]
                }
              }
            }
          ]
        }
      }
    ]
  }
}

对象包含的信息是好的,也是我所期望的。但是为什么嵌套这么奇怪呢?我从上述 PHP 代码中得到的结果对象如下所示:

[
    {
        "KasFloodDelay": 0.5,
        "ReturnString": "TRUE",
        "ReturnInfo": [
            {
                "mail_login": "xxxxxxx",
                "mail_password": "xxxxxxx",
                "mail_adresses": "xxxxxxx",
                ...
            }
        ]
    }
]

这就是我要的。

标签: javascriptphpnode.jssoap

解决方案


推荐阅读