调用腾讯天气 qq-weather

qq-weather

  1. 封装jsonp, 可以做成一个js工具函数
function jsonp(options) {
  // 动态创建script标签
  var script = document.createElement('script')
  // 拼接字符串的变量
  var params = ''

  for (var attr in options.data) {
    params += '&' + attr + '=' + options.data[attr]
  }

  // myJsonp0124741
  var fnName = 'myJsonp' + Math.random().toString().replace('.', '')
  // 它已经不是一个全局函数了
  // 我们要想办法将它变成全局函数
  window[fnName] = options.success
  // 为script标签添加src属性
  script.src = options.url + '?callback=' + fnName + params
  // 将script标签追加到页面中
  document.body.appendChild(script)
  // 为script标签添加onload事件
  script.onload = function () {
    document.body.removeChild(script)
  }
}
  1. 使用jsonp
jsonp({
  url: 'https://wis.qq.com/weather/common',
  data: {
    source: 'pc',
    // weather_type: 'forecast_1h',
    weather_type: 'forecast_1h|forecast_24h|observe',
    province: '江苏省',
    city: '苏州',
  },
  success: function (data) {
   console.log(data);
  },
})
  1. 作为测试
<html>
  <body>
    <div>
      receive <span id="qwerty"> </span>
    </div>
  </body>
  <script>
    function callfun(data) {
      document.getElementById('qwerty').innerHTML = JSON.stringify(data);
    }
  </script>
  <script src="https://wis.qq.com/weather/common?source=pc&weather_type=observe%7Cforecast_1h%7Cforecast_24h%7Cindex%7Calarm%7Climit%7Ctips%7Crise&province=%E6%B1%9F%E8%8B%8F%E7%9C%81&city=%E8%8B%8F%E5%B7%9E%E5%B8%82&county=&callback=callfun"></script>
</html>
  1. 详细解释课参考
    [https://www.zhihu.com/question/19966531]

作者:白马不是马

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中亿丰数字科技集团有限公司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值