使用Javascript调用Web服务 [英] Invoke web service using Javascript
本文介绍了使用Javascript调用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Java脚本访问Web服务,但它会产生错误:
NETWORK_ERR:XMLHttpRequest异常101
请帮我解决问题
Hi All I am trying to access the web service using java script but it gives flowing error :
NETWORK_ERR: XMLHttpRequest Exception 101
Please help me out to fix the issue
<script type="text/jscript" language="javascript">
var xmlHttp;
function SoapCall() {
// creatng the xmlHttp object
var xmlHttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// Calling the web service using post and true means asynchronous call
xmlHttp.open("post", "http://www.webservicex.net/CurrencyConvertor.asmx", false);
// Setting the request header to let the web service identify the soap request we would be sending
xmlHttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
xmlHttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/ConversionRate");
var soapRequest = '<?xml version="1.0" encoding="utf-16"?>' +
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
'<soap:Body>' +
'<ConversionRate xmlns="http://www.webserviceX.NET/">' +
'<FromCurrency>AFA</FromCurrency>' +
'<ToCurrency>AFA</ToCurrency>' +
'</ConversionRate>' +
'</soap:Body>' +
'</soap:Envelope>';
xmlHttp.send(soapRequest);
alert(xmlHttp.responseText);
}
</script>
推荐答案
听起来像跨域脚本问题...
http://stackoverflow.com/questions/2235929/network-error-xmlhttprequest-exception-101 [^ ]
在这些情况下,我通常会通过自己域中的服务将呼叫代理到第三者服务.因此,您的javascript会调用*您的*服务(您需要编写),该服务会将呼叫转发到第三方.
Sounds like a cross domain scripting problem...
http://stackoverflow.com/questions/2235929/network-error-xmlhttprequest-exception-101[^]
In these situations, I would normally proxy the call to the 3rd party service through a service on my own domain. So, your javascript calls *your* service (which you need to write) which simply forwards the call onto the 3rd party.
这篇关于使用Javascript调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文