<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
  <head>
  
    
    <title>My JSP 'WarningWeather.jsp' starting page</title>
  </head>
    
  <script type="text/javascript" src="../jquery-1.7.2.min.js"></script>  
   <script> 
$(document).ready(function(){ 
alert("is ok");
$.ajax({  
url: "http://m.weather.com.cn/data/101010100.html",
cache: true, 
type: "getJSON", 
success: function(js){ 

//document.write(js.weatherinfo.city); 
var str = "<B>" + js.weatherinfo.city + "<B/><br/>" ;
str +=js.weatherinfo.date_y + " " + js.weatherinfo.week + "<br/>"; 
str += "农历:" + js.weatherinfo.date + "<br/>"; 
str += "气温:" + js.weatherinfo.temp1 + "<br/>" ;
str += "风力:" + js.weatherinfo.wind1 + "<br/>" ;
str += "建议:" + js.weatherinfo.index_d + "<br/>" ;
$("div").html(str) ;
}, 
dataType : "json", 
}); 
});
</script> 
  <body>
  <div id="results"> 
    is ok
  </div> 
  </body>
</html>页面显示不出来  有代码写在后台,通过ajax调用显示天气预报

解决方案 »

  1.   

    除非目标站点允许跨域访问,否则是没戏的。浏览器会对此进行防护,避免出现安全漏洞。
    另一种情况是目标站点返回 jsonp 脚本,那么可以用(但你想访问的并不是):
    $.get("http://m.weather.com.cn/data/101010100.html",
      function(json) {
        alert(123);
      }, "jsonp"
    );
    具体参见:
    http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin