日期:2014-05-16  浏览次数:20680 次

JS 结合Ajax 异步获取数据显示当前城市天气 JS 总是报错“ 没有权限”??
 var xmlhttp;
    function getWeather() {    
        //获取用户输入的城市名称
          var mycity=document.getElementById("TextBox1").value;
        //创建异步对象
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        //加载服务器-注意URL参数的使用
 8         xmlhttp.Open("GET", "http://php.weather.sina.com.cn/search.php?city=" + mycity, true)    

        //异步对象事件挂钩
          xmlhttp.onreadystatechange=stateChange;
       //发送请求-无参数
          xmlhttp.Send(null);
    }
    function stateChange()
    {
       if(xmlhttp.readystate==4 && xmlhttp.status==200)
       {
          //获取所有返回的数据
          var data=bytes2BSTR(xmlhttp.ResponseBody);
          //过滤自己需要的数据
          var begin=data.indexOf("天气状况 begin");
          var end=data.indexOf("天气状况 end");
          var weather=data.substring(begin+15,end);
          //填充天气内容
          document.getElementById("divweather").innerHTML=weather;
          //显示结果
          document.getElementById("divweather").style.visibility="visible";
       }
    }

每次调试运行到第八行 
xmlhttp.Open("GET", "http://php.weather.sina.com.cn/search.php?city=" + mycity, true)    
的时候 总是会报错 “没有权限”网上查了下 IE里该设置的都设置了 还是不行
哪位高手指点下 !!!!!!!!!
感激不尽 在线等待中。。。。。。

------解决方案--------------------
跨域了,要用代理页面来处理,参考这个:天气预报

也是使用新浪的数据,但是需要做代理
------解决方案--------------------
跨域
同主域下的设置document.domain
不同主域的需要使用代理