通过Jsonp从中国天气网跨域获取天气预报信息

function get_weather(o) { //跨域获取天气信息
        var code = o.val();
        $.ajax({
            type: "get",
            async: false,
            cache: false,
            url: "get_json.php",
            dataType: "jsonp",
            data: {
                code_city: code
            },
            jsonpCallback: "weather_callback",
            beforeSend: function() {
                $(".box").html("<img src='loading.gif' />")
            },
            success: function(msg) {
                var w_str = "";
                var img2  = msg.weatherinfo.img2 != 99 ? "-<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img2 + ".gif' alt='' title=" + msg.weatherinfo.img_title2 + " />": "";
                var img4  = msg.weatherinfo.img4 != 99 ? "-<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img4 + ".gif' alt='' title=" + msg.weatherinfo.img_title4 + " />": "";
                var img6  = msg.weatherinfo.img6 != 99 ? "-<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img6 + ".gif' alt='' title=" + msg.weatherinfo.img_title6 + " />": "";
                var temp1 = sort_wd(msg.weatherinfo.temp1);
                var temp2 = sort_wd(msg.weatherinfo.temp2);
                var temp3 = sort_wd(msg.weatherinfo.temp3);
                w_str += "<p>"+ msg.weatherinfo.date_y+ " " + msg.weatherinfo.week+ " " + "所在城市:" + msg.weatherinfo.city + "</p>";
                w_str += "<p>今天:" + temp1 + " " + msg.weatherinfo.weather1 + "<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img1 + ".gif' alt='' title=" + msg.weatherinfo.img_title1 + " />" + img2 + "</p>";
                w_str += "<p>明天:" + temp2 + " " + msg.weatherinfo.weather2 + "<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img3 + ".gif' alt='' title=" + msg.weatherinfo.img_title3 + " />" + img4 + "</p>";
                w_str += "<p>后天:" + temp3 + " " + msg.weatherinfo.weather3 + "<img src='http://m.weather.com.cn/img/d" + msg.weatherinfo.img5 + ".gif' alt='' title=" + msg.weatherinfo.img_title5 + " />" + img6 + "</p>";
                $(".box").html(w_str).addClass("weather_box");
            },
            error: function() {
                $(".box").html('加载失败!');
            }
        });
    }

转载于:https://my.oschina.net/vipo/blog/198272

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值