我有一个调用 WSDL WebService 的 perl 脚本,我尝试将其移植到 JavaScript,但没有成功:( 我还需要对服务器进行身份验证才能获得我的响应。
我需要在我的 Phone Gap 项目中使用这部分代码。欢迎任何建议。
perl 脚本如下所示:
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);
请求如下所示:
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>
我尝试使用此代码从 JavaScript 获得相同的响应:
<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>
当我调用 JavaScript 函数时,我从 Firebug 得到这个响应:
> [Exception... "Access to restricted URI denied" code: "1012" nsresult:
> "0x805303f4 (NS_ERROR_DOM_BAD_URI)" ... { constructor={...}, code=1012, INDEX_SIZE_ERR=1, more...}
问候