|
今天试做了一个天气预报的功能!!用的是百度的api,没有的话自己去申请一个!!
本人初学者水平,js初学、php初学、css初学、wex5初学!!什么都会点,就是不精!!如果代码可以优化,请多指教!!
1、后端用PHP写的,主要是解决跨域问题,还有就是重新整理了json数据,分为两个:一个是天气json,一个是指数json:
- <?php
- if ($_GET['ac'] == 'getWeather') {
- header("Access-Control-Allow-Origin: *");
- $url = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%83%A0%E5%B7%9E&output=json&ak=你的AK";
- $data = file_get_contents($url);
- $obj = json_decode($data);
- echo json_encode($obj->results['0']->weather_data);
- }elseif ($_GET['ac'] == 'getZs') {
- header("Access-Control-Allow-Origin: *");
- $url = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%83%A0%E5%B7%9E&output=json&ak=你的AK";
- $data = file_get_contents($url);
- $obj = json_decode($data);
- echo json_encode($obj->results['0']->index);
- }
复制代码 2、在w页新建两个data,分别是weatherData和zsData
- //加载天气数据
- Model.prototype.weatherDataCustomRefresh = function(event) {
- $.support.cors = true;
- var weatherData = event.source;
- $.ajax({
- type : "GET",
- url : "http://127.0.0.1/test.php?ac=getWeather",
- dataType : 'json',
- async : false,//使用同步方式,目前data组件有同步依赖
- cache : false,
- success : function(data) {
- weatherData.loadData(data);//将返回的数据加载到data组件
- },
- error : function() {
- throw justep.Error.create("加载数据失败11");
- }
- });
- };
-
- //加载指数数据
- Model.prototype.zsDataCustomRefresh = function(event) {
- $.support.cors = true;
- var zsData = event.source;
- $.ajax({
- type : "GET",
- url : "http://127.0.0.1/test.php?ac=getZs",
- dataType : 'json',
- async : false,//使用同步方式,目前data组件有同步依赖
- cache : false,
- success : function(data) {
- zsData.loadData(data);//将返回的数据加载到data组件
- },
- error : function() {
- throw justep.Error.create("加载数据失败22");
- }
- });
- };
复制代码 3、剩下的就是list出来数据了,很简单吧!!!
效果
|
|