Popular blog tags

微信小程序,通用Http请求方式,及后台接收参数的方式(后台为 .net core)

1.
method:"GET"
contentType:"application/json"
[Query String Paramters]
core:
      func(dynamic  key)  or  Request.QueryString("key")
2.
method :"POST"
contentType:"application/x-www-form-urlencoded"
[Form Data]
core:
          HttpContext.Request.Form["key"].ToString()
3.
method:"POST"
contentType:"application/json"
[Payload]
core"
      func([FormBody] Dictionary <string string> object)

Node.js发送请求

  • Node.js发送请求,需要用到request这个模块
  • request官网
  • 先导入这个模块
npm install request --save

GET请求


var url = 'http://192.168.0.102:3000/home?name=xmg'

// 发送Get请求
// 第一个参数:请求的完整URL,包括参数
// 第二个参数:请求结果回调函数,会传入3个参数,第一个错误,第二个响应对象,第三个请求数据
request(url,function (error, response, data) {
   
    console.log(data)
    
});

Post请求

  • post请求有3种方式,由请求头中的content-type决定,属于哪一种post请求
    • application/x-www-form-urlencoded: 普通http请求方式,参数是普通的url参数拼接
    • application/json: JSON请求方式,参数是json格式
    • multipart/form-data: 文件上传

application/x-www-form-urlencoded

var url = 'http://192.168.0.102:3000/home?name=xmg'

request.post({url:url, form:{key:'value'}}, function(error, response, body) {
    if (!error && response.statusCode == 200) {
    }
})

application/json

var url = 'http://192.168.0.102:3000/home'

request({
    url: url,
    method: "POST",
    json: true,
    headers: {
        "content-type": "application/json",
    },
    body: JSON.stringify(requestData)
}, function(error, response, body) {
    if (!error && response.statusCode == 200) {
    }
}); 

multipart/form-data


var url = 'http://192.168.0.102:3000/home'
var formData = {
    // Pass a simple key-value pair
    my_field: 'my_value',
    // Pass data via Buffers
    my_buffer: new Buffer([1, 2, 3]),
    // Pass data via Streams
    my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
};
request.post({url:url, formData: formData}, function (error, response, body) {  
    if (!error && response.statusCode == 200) {
    }
})


小程序客户端
  //  ----发送选择的股票和日期 --- 
  sendStockCode: function(e) {
    var that = this;

    var stockCode = this.data.inputValue;

    //请求网络数据
    wx.request({
      url: "https://api.usdotnet.com/api/values",
      success: function(res) { //请求成功
        console.log(res); //在调试器里打印网络请求到的json数据
      },
      fail: function(res) { // 请求失败
      }
    });

    wx.request({
      url: 'https://api.usdotnet.com/api/stockinfo', //本地服务器地址
      data: {
        stockCode: this.data.inputValue,
        date: 'abc',
      },
      method: 'GET',
      header: {
        'content-type': 'application/json' //默认值 
      },
      success: function(res) {

        console.log(" wx.request({url: 'https://api.usdotnet.com/api/stockinfo:" + res);

        that.setData({
          stockInfo: res.data
        })

        //传入给画布要用到的参数
        preStockClose = res.data.stock_yesterday_m_fClose; //股票昨日收盘价

        pre_shanghaiCompositeIndexClose = res.data.shanghaiCompositeIndex_yesterday_m_fClose; //上海指数昨日收盘价
        firsttime = res.data.firsttime;
      },
      fail: function(res) {
        console.log("失败");
      }
    })


    //点击发送,调用服务器上的 sendMessage 方法
    this.hubConnect.send("SendMessage", stockCode, "20190401");

    // 点击发送,调用服务器上的 sendMessage 方法
    //  this.hubConnect.send("SendMessagedp", "SZ002930", "20190401");



  },