卡路里计算器微信小程序前后端交互

SSM框架
在Controller中添加一个

@RequestMapping(value="/getjson",method={RequestMethod.GET})
@ResponseBody
public RespondResult toJson(HttpServletRequest request) throws Exception{
RespondResult rr = new RespondResult();
String sex = request.getParameter("Sex");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
String age = request.getParameter("age");
String sportIndex = request.getParameter("sportIndex");
String aerobic = request.getParameter("aerobic");
String goal = request.getParameter("Goal");
String energy = request.getParameter("energy");
Student student = new Student();
student.setName(sex);
student.setQq(height);
student.setLink(weight);
student.setSchool(age);
student.setSource(sportIndex);
student.setStId(aerobic);
student.settA(goal);
student.setType(energy);
studentService.addStudent(student);
Integer basic =null;
Integer need=null;
Integer prot =null;
Integer fat =null;
Integer cab =null;
if(sex.equals("male")){
basic = (int) (90 + 4.8* Integer.parseInt(height) + 13.4* Integer.parseInt(weight) - 5.7* Integer.parseInt(age));
}
else {
basic = (int) ( 450 + 3.1* Integer.parseInt(height) + 9.2* Integer.parseInt(weight) - 4.3* Integer.parseInt(age));
}
Integer total =(int) (basic * Double.parseDouble(sportIndex)+Integer.parseInt(aerobic));
if(goal.equals("muscle")){
need = ( total + Integer.parseInt(energy));
prot =(int) ( 2.2 * Integer.parseInt(weight));
fat =  (int) (need * 0.25 / 9 );
cab =  ((need - prot*4 -fat*9)/4);
}else{
need =  ( total - Integer.parseInt(energy));
prot = (int) ( 2.75 * Integer.parseInt(weight));
fat = (int) ( need * 0.2 / 9) ;
cab =  ((need - prot*4 -fat*9)/4);
}
rr.setBasic(basic);
rr.setCab(cab);
rr.setFat(fat);
rr.setNeed(need);
rr.setProt(prot);
rr.setTotal(total);
return rr;
}

在小程序前端 index.js

     wx.request({
        url: 'https://www.tomwoo.tk/CounterWebApp/calory/getjson',
        data: {
          height: e.detail.value.height,
          weight: e.detail.value.weight,
          age: e.detail.value.age,
          aerobic: e.detail.value.aerobic,
          energy: e.detail.value.energy,
          Sex: e.detail.value.sex ,
          Goal: e.detail.value.goal,
          sportIndex: si,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
        }
      })

res返回一个json数据

推荐阅读更多精彩内容