首页 > 代码库 > 跨域获取json数据

跨域获取json数据

这是天气json的数据,这里是链接,json的数据接口  http://m.weather.com.cn/data/101010100.html

json的数据格式

{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年3月4 日","date":"","week":"星期二","fchh":"11","cityid":"101010100","temp1":"8℃~- 3℃","temp2":"8℃~-3℃","temp3":"7℃~-3℃","temp4":"8℃~- 1℃","temp5":"10℃~1℃","temp6":"10℃~2℃","tempF1":"46.4℉~26.6℉","tempF2":"46.4℉~26.6℉","tempF3":"44.6℉~26.6℉","tempF4":"46.4℉~30.2℉","tempF5":"50℉~33.8℉","tempF6":"50℉~35.6℉","weather1":" 晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"多 云","weather6":"多 云","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"0","img_title1":" 晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":" 晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"多 云","img_title10":"多云","img_title11":"多云","img_title12":"多 云","img_title_single":"晴","wind1":"北风4-5级转微风","wind2":"微风","wind3":"微 风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"北风","fx2":"微 风","fl1":"4-5级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3 级","fl6":"小于3级","index":"寒冷","index_d":"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者 尤其要注意保暖防冻。","index48":"冷","index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜 着厚棉衣、冬大衣或厚羽绒服。","index_uv":"中等","index48_uv":"中等","index_xc":"较适 宜","index_tr":"一般","index_co":"较舒适","st1":"7","st2":"- 3","st3":"8","st4":"0","st5":"7","st6":"-1","index_cl":"较不 宜","index_ls":"基本适宜","index_ag":"易发"}}

这里是什么不用说了吧

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Json跨域解决办法</title>
<script src=http://www.mamicode.com/‘http://code.jquery.com/jquery-1.8.0.js‘ type=‘text/javascript‘></script>
<script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/blob/master/json2.js"></script>
</head>
<body>
<div id="content">
</div>
<script>
         $(function () {
             $.getJSON("http://query.yahooapis.com/v1/public/yql", {
                 q: "select * from json where url=\"http://m.weather.com.cn/data/101010100.html\"",
                 format: "json"
             }, function (data) {
                 if (data.query.results) {
                     $("#content").text(JSON.stringify(data.query.results));
                     var J_data = http://www.mamicode.com/JSON.parse(JSON.stringify(data.query.results));
                     alert(J_data.weatherinfo.city);
 
                 } else {
                     $("#content").text(‘no such code: ‘ + code);
                 }
             });
         });
     </script>
</body>
</html>