我有一个调用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...}
此致
答案 0 :(得分:0)
此错误的原因是浏览器限制进行跨域调用。您只能回拨给为您提供HTML / JavaScript的服务器。
您需要一个支持JSONP的代理服务器或服务提供商来使用跨域服务。