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>
原文链接:/json/290364.html