jsonp技術訪問天氣Demo,該網站實現了返回數據爲xml或json類型
注意:select*fromjsonwhereurl='..'要用空格隔開
http://query.yahooapis.com/v1/public/yql?q=select*fromjsonwhereurl='http://m.weather.com.cn/data/101010100.html'
//返回一個xml的文檔樹(object類型)
http://query.yahooapis.com/v1/public/yql?q=select*fromjsonwhereurl='http://m.weather.com.cn/data/101010100.html'&format=json
//將返回的xml的文檔樹(object類型)轉變爲json類型(object)
tianqi.jsp代碼如下:
<%@pagepageEncoding="utf-8"%>
<%@tagliburi="/struts-tags"prefix="s"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>註冊</title>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scripttype="text/javascript"src="../js/jquery-1.4.3.js"></script>
</head>
<body>
<fontcolor='red'>天氣預報demo界面</font>
<divid="content">
文本框內容
</div>
<script>
$(function(){
$.getJSON("http://query.yahooapis.com/v1/public/yql",
{q:"select*fromjsonwhereurl='http://m.weather.com.cn/data/101010100.html'",format:"json"},
function(data){
var$content=$("#content")
if(data.query.results){
varresult=JSON.stringify(data.query.results);
$content.text(result);
varobj=eval('('+result+')');
alert(obj.weatherinfo.city);
}else{
$content.text('nosuchcode:'+code);
}
});
});
</script>
</body>
<script>
</script>
</html>