如何使用HttpPlugin ionic2在体内传递XML数据 [英] How can I pass XML data in body with HttpPlugin ionic2

查看:420
本文介绍了如何使用HttpPlugin ionic2在体内传递XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XML 中调用一个接受 XML 请求和响应的服务,所以我在下面做了代码

I am calling one service which accept XML request and response also in XML so for that I have done below code

let xmlBody = '<ServiceRequest>' +
        '<SRNumber>123456' +
        '</SRNumber>' +
        '</ServiceRequest>';


    this.httpPlugin.setHeader('authorization', "Bearer " + token);
    this.httpPlugin.setHeader('content-type', "application/xml");
    this.httpPlugin.post('https://xxx.test.server/Service', xmlBody, {}).then((response) => 
{

})

根据上述请求,它总是返回错误状态:500

Based on above request its always return with error status:500

我认为我的身体没有到达服务器

I think my body not reaching to server

任何帮助我如何申请 XML 正文通过HttpPlugin?

Any help how can I request XML body through HttpPlugin ?

推荐答案

经过大量的R& D终于我得到了通过的解决方案我的请求中的 XML 检查以下代码以获取更多详细信息:

After lots of R&D finally I got solution to pass XML in my request check below code for more detail :

          let headers = {
            "Content-type": 'application/xml',
             "Authorization": "Bearer " + token,
          };

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

          this.httpPlugin.setDataSerializer('json');

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

在上面的代码中我们需要遵循2-3步骤,这将导致 XML 请求在服务器上成功发送。

In above code we need to follow 2-3 step which will lead to XML request successfully send on server.

1st :在 JSON中设置标题格式化不使用插件的setHeader方法。

1st : Set header in JSON formate not use setHeader method of plugin.

第二: 设置 XML 正文,并在其中添加新行。

2nd : Set XML body with add new line in it as above.

第3名:这一点是没有此代码的补丁将无法正常工作

3rd : This point is kind of patch without this code will not work

  this.httpPlugin.setDataSerializer('json');

通过这个我们可以成功获得 XML的响应基于API。

By this we can successfully get response on XML based API.

帮助来源: https://github.com/silkimen/cordova-plugin-advanced-http/issues/34


注意:XML解析使用xml2js插件

Note : XML parsing use xml2js plugin

希望这可以帮助一个面对同样的人像我这样的问题。

hope this will help some one who is facing same issue like me.

这篇关于如何使用HttpPlugin ionic2在体内传递XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆