signed

QiShunwang

“诚信为本、客户至上”

微信小程序与Java后台连接 get与post请求:

2020/12/26 16:28:11   来源:

微信小程序与Java后台连接
get与post请求:
1.get请求:
wxml中的代码

刷新
接收到后台的数据:{{theme}}

js代码:
bindtest: function(options) {
var that = this;
wx.request({
url: ‘http://localhost:8090/insertStudent’, //本地服务器地址
data: { //data中的参数值就是传递给后台的数据
transInfo: '呐,这是小程序端给后台的数据
},
method: ‘GET’,
header: {
‘content-type’: ‘application/json’ //默认值
},
success: function(res) { //res就是接收后台返回的数据
that.setData({
tt: res.data
})
console.log(res.data);
},
fail: function(res) {
console.log(“失败”);
}
})
},
2.post请求
bindtest: function(options) {
var that = this;
wx.request({
url: ‘http://localhost:8090/student/insertStudent’, //本地服务器地址
header:{
‘content-type’:‘application/json;charset=utf-8’
},
method: ‘POST’,
data: JSON.stringify({ //data中的参数值就是传递给后台的数据
// transInfo: ‘呐,这是小程序端给后台的数据’
“username”:‘abc123’,
“password”:‘123’,
“name”:‘asd’,
“gender”:‘1’,
“phone”:‘123456’,
“join_num”:234,
“publish_num”:234

  }),  
  success: function (res) {
     console.log(res);
    if (res.data.ret == 200) { 

     //something to do  
    }
    else{ 
     //something to do
    } 
  },  
fail: function (res) {
    console.log(res);

  }
})
}