Ajax代码可在IE9中使用,但不能在其他浏览器中使用 [英] Ajax Code works in IE9 but nor works in other browser
本文介绍了Ajax代码可在IE9中使用,但不能在其他浏览器中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过Java脚本调用Web服务,它与IE 9兼容,但不适用于其他浏览器.请帮助我解决问题
Hi ,
I am trying to invoke web service via java script It works great with IE 9 but not works on other browsers Please help me to fix the issue
<script type="text/javascript" language="javascript">
var xmlHttp;
function SoapCall() {
// creatng the xmlHttp object
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", true);
// 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("Origin", "http://www.webserviceX.NET");
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "http://172.27.47.144");
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>USD</FromCurrency>' +
'<ToCurrency>INR</ToCurrency>' +
'</ConversionRate>' +
'</soap:Body>' +
'</soap:Envelope>';
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
}
xmlHttp.send(soapRequest);
}
</script>
推荐答案
如果这在其他浏览器中不起作用,则您的SVC似乎已经支持此功能,但是请确保使用Fiddler2进行查看发生了什么事.
Access-Control-Allow-Origin标头用于请求的资源,而不用于请求该资源的页面.
If this doesn''t work in the other browsers , then your SVC seems to already be supporting this, but to be sure, use Fiddler2 to see what is going on.
The Access-Control-Allow-Origin header is used on the resource being requested, not on the page requesting it.
这篇关于Ajax代码可在IE9中使用,但不能在其他浏览器中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文