【问题标题】:Body error: Implementing ngx-soap client in Angular 4正文错误:在 Angular 4 中实现 ngx-soap 客户端
【发布时间】:2018-02-13 14:05:33
【问题描述】:

我正在尝试在我的 Angular 4 应用程序中实现 ngx-soap 包 here。我正在使用 Node 服务器并尝试使用此包 here 创建soap客户端和服务器文件,并在我从这里获得的帮助下成功实现。现在我要在前端制作客户端。我已经定义了body,但不确定是否正确。

import {Component, OnInit} from '@angular/core';
import {SOAPService, Client} from 'ngx-soap';
import {Http} from '@angular/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {

  jsonResponse: any;
  xmlResponse: string;
  loading: boolean;
  message: string;
  private client: Client;

  constructor(private http: Http, private soap: SOAPService) {}

  ngOnInit() {
    this.http.get('/assets/check_username.wsdl').subscribe(response => {
      if (response && response.text()) {
        this.soap.createClient(response.text()).then((client: Client) => {
          this.client = client;
        });
      }
    });
  }

  client_wsdl() {
    this.clear();
    this.loading = true;

   let body = {
         userName:"TEST_USER"
    };

    this.client.operation('checkUserName', body)
      .then(operation => {
        if (operation.error) {
          console.log('Operation error', operation.error);
          return;
        }

        let url = operation.url.replace("http://www.examples.com/", "/CheckUserName");

        this.http.post(url, operation.xml, {
          headers: operation.headers
        }).subscribe(
          response => {
            this.xmlResponse = response.text();
            this.jsonResponse = this.client.parseResponseBody(response.text());

            try {
              this.message = this.jsonResponse.Body.CheckUserNameResponse;
            } catch (error) {}
            this.loading = false;
          },
          err => {
            console.log("Error calling ws", err);
            this.loading = false;
          }
        );
      })
      .catch(err => console.log('Error', err));
  }

}

在节点中,我有服务器文件,我试图从 arangodb 访问查询,一旦我运行我的客户端文件,我就可以在控制台中打印出结果信封。我想做同样的事情,但来自 Angular 4.

server.js

var soap = require('strong-soap').soap;
var http = require('http');

var myService = {
  CheckUserName_Service: {
    CheckUserName_Port: {
      checkUserName: function(args, soapCallback) {

        console.log('checkUserName: Entering function..');
        db.query(aqlQuery `
                LET startVertex = (FOR doc IN spec
                FILTER doc.serial_no == '"123456abcde"'
                LIMIT 2
                RETURN doc
                )[0]

                FOR v IN 1 ANY startVertex belongs_to
                RETURN v.ip`, {
            bindVar1: 'value',
            bindVar2: 'value',
          }).then(function(response) {
            console.log(`Retrieved documents.`, response._result);
            soapCallback(JSON.stringify(response._result));
          })
          .catch(function(error) {
            console.error('Error getting document', error);
            soapCallback('Error getting document' + error.message);
          });
      }
    }
  }
};
var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');

var server = http.createServer(function(request, response) {
  response.end("404: Not Found: " + request.url);
});

var port = 8000;
server.listen(port);

var soapServer = soap.listen(server, '/test', myService, xml);
soapServer.log = function(type, data) {
  console.log('Type: ' + type + ' data: ' + data);
};

console.log('SOAP service listening on port ' + port);

因为我是使用后端服务的新手。我无法解决基本错误。我没有掌握正确的窍门。比如如何处理它?

错误

【问题讨论】:

  • 查看您对this 的所有使用情况。 this(即 AppComponent)上是否存在这些属性?
  • @PaulSamsotha 是的,这是一个错误..,我没有正确导入包。,我现在会更正

标签: node.js angular web-services soap wsdl


【解决方案1】:

首先为了解决..,我重新编辑了我在问题中更新的代码。然后出现跨域错误。这可以通过在代码中添加插件或标头来解决。

那我就用下面的函数了。

function dbQuery() {
          var response = db.query(aqlQuery`
                 LET startVertex = (FOR doc IN spec
                 FILTER doc.serial_no == '"123456abcde"'
                 LIMIT 2
                 RETURN doc
                 )[0]

                FOR v IN 1 ANY startVertex belongs_to
                RETURN v.ip`,
                {
                bindVar1: 'value',
                bindVar2: 'value',
                });
console.log("response is "+ response);
    return response;
}

//usage inside another async function

function main2() {
        var result2 = dbQuery();

        return result2.then(function(test){

                console.log("test is "+JSON.stringify(test._result));
                var IP = test._result.filter(Boolean);
                console.log("Ip is "+IP);
});
}
main2();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多