关于JavaScript 访问基于SOAP的WebService跨域问题

最近,搭建好了一个基于SOAP的WebService服务器。结果,使用JavaScript访问WebService遇到了跨域的问题!

首先,声明代码中的WebService不是本人搭建,由于本人的服务器地址不方便公开。

这个是测试HTML

复制代码

<html>
    <meta charset="utf-8" />
    <head>
        <title>通过ajax调用WebService服务</title>
        <script>
            
            var xhr = new XMLHttpRequest();
            function sendMsg(){
                var name = document.getElementById('name').value;
                //服务的地址
                var wsUrl = 'http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx';
                
                //请求体
                var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + 
                                     ' <soapenv:Body><getRegionCountry xmlns="http://WebXml.com.cn/" /></soapenv:Body> </soapenv:Envelope>';
                                     
                //打开连接
                xhr.open('POST',wsUrl,true);
                
                //重新设置请求头
                xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
               
                xhr.setRequestHeader("SOAPAction","http://WebXml.com.cn/getRegionCountry");
                
                //设置回调函数
                xhr.onreadystatechange = _back;
                
                //发送请求
                xhr.send(soap);
            }
            
            function _back(){
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                            //alert('调用Webservice成功了');
                            var ret = xhr.responseXML;
                            var msg = ret.getElementsByTagName('return')[0];
                            document.getElementById('showInfo').innerHTML = msg.text;
                            //alert(msg.text);
                        }
                }
            }
        </script>
    </head>
    <body>
            <input type="button" value="发送SOAP请求" οnclick="sendMsg();">
            <input type="text" id="name">
            <div id="showInfo">
            </div>
    </body>
</html>

复制代码

浏览器按下F12以后:

出现

OPTIONS http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx 403 (Forbidden) 
OPTIONS http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access. 
XMLHttpRequest cannot load http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access. 

目前,看网上都是通过JSONP,即src服务器端JS,然后通过回调函数传递数据。

但是,这个得有个前提:服务器端得配合你啊,假如,你与服务器端不是一伙人,那么协调是个问题啊!

另外,对于服务器端来讲,这种做法也不安全哇!

对于JavaScript 的同源策略,确实很好的提高了安全性!但是,也限制了移动端开发纯HTML5+JS的APP!

目前,我已经放弃使用JavaScript 访问 WebService了,我使用纯HTML5开发移动APP的做法,也值得商榷!

保险的做法是,HTML5+JS做界面,原生语言做后端!

那么,我看不出HTML5+JS 比 Android的XML, WP的XLAM好了很多。。。

此外,我对于Qt的跨平台,表示出极大的看好!

希望,能有个大神指点我一下JavaScript 跨域问题,既保证客服端的便捷,也保证服务器端的安全!

2014-12-19

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值