美文网首页
微信小程序与web api 交互

微信小程序与web api 交互

作者: 觉释 | 来源:发表于2024-04-02 13:35 被阅读0次

方式1: 传输json字符串

微信小程序端传输json 到web api 后台

formSubmit: function (e) {
  var that = this
   var  data={
      username: e.detail.value.username,
      phone:e.detail.value.phone,
      openid: e.detail.value.openid,
      sessionId:e.detail.value.sessionId 
  };
  
  console.log('jsonstr',JSON.stringify(data));
  wx.request({
    url:'http://XXX/api/access/bind', 
    //header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: data,
    method: 'POST',
    success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          wx.showToast({
            title: "绑定成功",
            duration: 2000
          });
        }else{
            wx.showToast({
              title: '绑定失败,请联系管理员检查是否存在账号',
              duration: 2000
            });
          }
          that.setData({
            //Description4: res.data.data
          })
     },
     fail(err){
      console.error(err);
     }
})
}
 

web api 接收
 
 [HttpPost]
 [Route("api/access/bind")]
 public int bind([FromBody] Object str)
 {
     int i= 0;
     接收json 字符串再进行转换解析
     return i;
 }

方式2:直接拼接url

handleClick3() {
   var that=this;
 
  var code=3;
        var u='http://127.0.0.1:44303/api/product/GetByCode?code='+code;
        wx.request({  
          url: u,
         // header: { 'Content-Type': 'application/json;charset=utf-8' },
          method:"POST",
          success: function(res) {  
            console.log('请求成功', res.data);
          
            that.setData({
              name: res.data.Name,
             
            })
          },  
          fail: function(err) {  
            console.log(u)  
            console.error('请求失败',err);
          }  
        })
},

后台:

   // GET api/GetByCode
   [HttpPost, HttpGet]
   [Route("api/product/GetByCode")]
   public Product GetByCode( string code)
   {
       string tempcode = code;
       Product shinva_QM = new Product();
       string sql = "select * from Product where code='" + tempcode + "'";
    return    SqlDapperHelper.ExecuteReaderReturnT<Product>(sql, null, true, null);
    
   }


相关文章

网友评论

      本文标题:微信小程序与web api 交互

      本文链接:https://www.haomeiwen.com/subject/kxewtjtx.html