2
我想通過使用Ajax來使用Restful Web服務進行貨幣轉換。在這裏,我無法解析或讀取由Restful webservice生成的xml文件中的數據,請仔細閱讀我的代碼並幫助我。如何在ajax中調用RESTful Web服務?
代碼:雖然你沒有提供
<html>
<head>
<script>
var xmlHttp
function showCust()
{
// alert("function Called");
xmlHttp = new XMLHttpRequest();
// alert("function Called");
//var fromCurr = document.getElementById("name");
var fromCurr = f1.elements[0].value;
var toCurr = f1.elements[1].value;
// alert("function Called");
//alert(fromCurr + " " +toCurr);
var url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + fromCurr + "&ToCurrency=" + toCurr;
alert(url);
xmlHttp.open("GET", url, true);//it will call 'CustomerDetails' servlet
// alert("function Called");
xmlHttp.onreadystatechange = stateChanged;//this is userdefined method which will fired based readyState property
xmlHttp.send();
}
function stateChanged()
{
// alert("function Called");
//check whether the response is completely loaded or not?
if (xmlHttp.readyState == 4)
{
var str1 = xmlHttp.responseText;
alert(str1);
//var myObject = eval('(' + str1 + ')');
//var text = '{"name":"Sivarmayya","email":"[email protected]","phone":"9000387222"}'
var users = xml.getElementsByTagName("double");
alert(users);
var user = users[0];
//var obj = JSON.parse(str1);
//document.f1.mail.value=obj.mail;
}
}
</script>
</head>
<body>
<form name="f1" >
From Currency: <input type="text" title="enter from currency" id="name"/><br>
To Currency: <input type="text" title="enter to currency" id="name1"/></br>
<input type="submit" value="Go" onclick="return showCust()">
Result: <input type="text" name="mail" /><br>
</form>
</body>
我沒有得到任何錯誤。問題是我無法從使用指定網址生成的xml文件中獲取數據.. –
如何使用ajax啓用瀏覽器的CORS –
做好你的功課,google吧!關鍵字'enable cors'在第一頁上給出了很多相關的結果。無論如何,你可以從http://www.html5rocks.com/en/tutorials/cors/ –