weixin_33692284 2013-11-01 05:59 采纳率: 0%
浏览 46

PhoneGap呼叫WSDL

I have a perl script that call a WSDL WebService and I try to port it to JavaScript, without any success :( I also need to authenticate to the server in order to get my response.

I need to use this section of code in my Phone Gap project. Any suggestion is welcome.

The perl script looks like:

my $url             = "https://$user:$pass\@pathToMyExternalWSDL.xml?VERSION=1.1&STYLE=style";
my $soap            = SOAP::Lite->service($url)->autotype(1)->readable(1)->on_fault( sub
    {
        my $soap = shift;
        my $res  = shift;
        if(ref($res) eq '')
        {
            warn ($res);
        }
        else
        {
            warn($res->faultstring);
        }
        return new SOAP::SOM;
    });

sub SOAP::Transport::HTTP::Client::get_basic_credentials{return $user=>$pass;}
my $response = $soap->action($param1, $param2);

The request looks like:

Accept: text/xml
Accept: multipart/ *
Accept: application/soap
Content-Length: 926
Content-Type: text/xml; charset=utf-8
SOAPAction: "action"

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
    ...
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <action>
      <param1 xsi:type="data_type1">myParam1</param1>
      <param2 xsi:type="data_type2">myParam2</param2>
    </wc:action>
  </soap:Body>
</soap:Envelope>

I try to get the same response from JavaScript with this cod:

    <script type="text/javascript">
        function soap() {
            try {
                var xmlhttp = new XMLHttpRequest();
                var action = "action"; // same action like the script
                var sURL= "https://user:password@pathToMyExternalWSDL.xml?VERSION=1.1&STYLE=style"; //same url like the script
                xmlhttp.open("POST", sURL, false);
                xmlhttp.setRequestHeader("Accept", "text/xml");
                xmlhttp.setRequestHeader("Accept", "application/soap");
                xmlhttp.setRequestHeader("Accept", "multipart/*");
                xmlhttp.setRequestHeader("Content-Length", "926");
                xmlhttp.setRequestHeader("SOAPAction", action);
                xmlhttp.setRequestHeader("Content-Type", "text/xml");
                xmlhttp.setRequestHeader("Content-Type", "charset=utf-8");

                var SOAPEnvelope = "<?xml version='1.0' encoding='UTF-8'?> ";
                SOAPEnvelope += "<soap:Envelope xmlns:mime='http://schemas.xmlsoap.org/wsdl/mime/' "; 
                SOAPEnvelope += "soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' "; 
                SOAPEnvelope += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "; 
                SOAPEnvelope += "xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' "; 
...
                SOAPEnvelope += "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "; 
                SOAPEnvelope += "xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";
                SOAPEnvelope += "<soapenv:Header/>";
                SOAPEnvelope += "<soapenv:Body>";
                SOAPEnvelope += "<wc:action>";
                            SOAPEnvelope += "<param1 xsi:type="data_type1">myParam1</param1>";
                            SOAPEnvelope += "<param2 xsi:type="data_type2">myParam2</param2>";
                            SOAPEnvelope += "</wc:action>";
                SOAPEnvelope += "</soapenv:Body>";
                SOAPEnvelope += "</soapenv:Envelope>";                  
                xmlhttp.send(SOAPEnvelope);
            }
            catch (e) {
            console.log(e);
        }
        //var tmpXML = xmlhttp.responseXML.xml
        }
    </script>

When I invoke JavaScript function I get this response from Firebug:

> [Exception... "Access to restricted URI denied" code: "1012" nsresult:
> "0x805303f4 (NS_ERROR_DOM_BAD_URI)" ... { constructor={...}, code=1012, INDEX_SIZE_ERR=1, more...}

Regards

  • 写回答

1条回答 默认 最新

  • weixin_33692284 2014-04-02 10:34
    关注

    The reason for this error is the restriction in browsers to do cross domain calls. You can only call back to the server that has provided you with HTML/JavaScript.

    You will need a proxy server or a service provider that supports JSONP to consume cross domain services.

    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加