【问题标题】:How to call RESTful Web Service in ajax?如何在 ajax 中调用 RESTful Web 服务?
【发布时间】:2015-07-31 10:50:09
【问题描述】:

我正在尝试通过 Ajax 使用 Restful Web Service 进行货币转换。这里我无法解析或读取由 Restful webservice 生成的 xml 文件中的数据,请查看我的代码并帮助我。

代码:

<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":"smadhupada@miraclesoft.com","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:&nbsp; <input type="text" title="enter from currency"  id="name"/><br> 
        To Currency:&nbsp; <input type="text" title="enter to currency"  id="name1"/></br>
        <input type="submit" value="Go" onclick="return showCust()">
        Result:&nbsp;&nbsp;<input type="text" name="mail" /><br> 
    </form> 
</body> 

【问题讨论】:

    标签: ajax html web-services restful-authentication


    【解决方案1】:

    虽然您没有提供您遇到的错误类型,但我认为您的问题是您的浏览器阻止了 CORS 请求:http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

    【讨论】:

    • 我没有收到任何错误。问题是我无法从使用指定 url 生成的 xml 文件中获取数据..
    • 如何使用ajax开启浏览器的CORS
    • 做你的功课,谷歌它!关键字enable cors 在第一页就给出了很多相关的结果。无论如何,你可以从html5rocks.com/en/tutorials/cors开始
    • 我通过执行以下操作获得了在 Internet Explorer 浏览器中启用 cors 的解决方案:
    • 即您可以通过工具 | 在 IE 中添加受信任站点互联网选项 |安全选项卡。然后您可以通过自定义级别设置受信任站点的安全性 |杂项。您会看到,默认情况下,“跨域访问数据源”选项设置为提示。但是,Prompt 并不令人满意,因为它会中断任何当前正在运行的应用程序,并带有一个消息框,要求用户确认他们想要访问该服务。相反,将选项设置为启用。但是我没有找到在其他浏览器中启用 CORS 的解决方案......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2011-08-28
    • 2014-03-14
    相关资源
    最近更新 更多