9

我正在使用cordova-plugin-advanced-http插件进行 API 调用,所有JSON启用的 API 都可以正常工作,但是我有一个XML嵌入式 API,它在 Postman 中工作正常,但是当我从 ionic 调用它时,它的参数没有到达服务器端。

下面是我的 XML API 代码:

类型 1:

let headers = {
          "Content-type": 'text/xml; charset=utf-8',
          "Authorization": token,
        };

    let xmlBody =
      '<ServiceRequest>' +
      '<CaseNumber>' + caseNumber +
      '</CaseNumber>' +
      '</ServiceRequest>'

    this.httpPlugin.setDataSerializer('utf8');

    this.httpPlugin.post('https://test.com/Service', xmlBody, headers).then((response) => {
      console.log("XML Response : ", JSON.stringify(response.data));
      xml2js.parseString(response.data, function (err, result) {
        console.log("XML parser success:", result);
        console.log("XML parser error:", err);
        if (result) {
          resolve(result);
        } else {
          reject(err);
        }

      });
    }).catch(error => {
      if (error.status == 403) {
        console.log("Token expired : " + JSON.stringify(error));
      } else {
        console.log("Error : " + error.error);
        console.log("Error " + JSON.stringify(error));
        reject(error);
      }
    });

类型 2:

    let xmlBody = '<ServiceRequest>' +
      '<CaseNumber>' + caseNumber +
      '</CaseNumber>' +
      '</ServiceRequest>';

    console.log("XML Body", xmlBody)

    // this.httpPlugin.setRequestTimeout(60);
    this.httpPlugin.setDataSerializer('utf8');

    this.httpPlugin.setHeader('*', 'authorization', token);
    this.httpPlugin.setHeader('*', 'Content-Type', "application/x-www-form-urlencoded");

    this.httpPlugin.post('https://test.com/Service', xmlBody, {}).then((response) => {
      console.log("XML Response : ", JSON.stringify(response.data));
      xml2js.parseString(response.data, function (err, result) {
        console.log("XML parser success:", result);
        console.log("XML parser error:", err);
        if (result) {
          resolve(result);
        } else {
          reject(err);
        }

      });

    }).catch(error => {
      if (error.status == 403) {
        console.log("Token expired : " + JSON.stringify(error));
      } else {
        console.log("Error : " + error.error);
        console.log("Error " + JSON.stringify(error));
        reject(error);
      }

    });

它一直在从服务器抛出错误并且使用相同的请求,我能够在邮递员和本机 iOS 代码中获取数据。

我在 GitHub 上提到了这个问题,但仍然没有成功。

尽管无法在服务器上获取数据,但我缺少一些东西。

帮我解决这个问题。

4

1 回答 1

0

在这个问题上苦苦挣扎之后,我找到了一个清理我的请求 cookie 的解决方案。

在 HTTP Advanced 插件中,有一种方法可以清除我的 cookie。

clearCookies()

清除所有 cookie。

在调用任何 API 之前使用此方法。

因此,它将清除我所有的 cookie 以及与旧 cookie 相关的问题将通过这种方式解决。

constructor(
    public storage: Storage,
    public httpPlugin: HTTP,
    private platform: Platform
  ) {
    // enable SSL pinning
    httpPlugin.setSSLCertMode("pinned");
    //Clear old cookies
    httpPlugin.clearCookies();
  }

上面的代码解决了我的问题。

感谢大家的快速指导和建议。

如果这不是清除我的旧请求数据的正确方法,请对此发表评论。

于 2019-03-08T05:43:42.760 回答