微信小程序-获取request响应(附教程分享)

第一次写微信小程序在开发过程中,想要定义一个函数请求天气api并返回数据

思路代码(错误示例)

function getWeatherData(cityName){
 
  var data={};
  wx.request({
    url: 'https://www.sojson.com/open/api/weather/json.shtml?city=' + cityName,
    success:function(res){
      data = res.data;
    }
  });
    return data;
}

这种写法是不正确的,并不能将响应的结果传给data

后改成这种写法

function getWeatherData(cityName,callback){
  var rr={};
  wx.request({
    url: 'https://www.sojson.com/open/api/weather/json.shtml?city=' + cityName,
    success:function(res){
      callback(res.data);
    }
  });

}

在callback中对响应的数据进行处理

感谢刘明华先生的分享,好东西应该分享出来。

开发教程:https://pan.baidu.com/s/1yJieXrVZOlTe8i4KRPrJUQ?from=singlemessage&errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0&traceid=#list/path=%2F

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/81560547