• 微信小程序连接Java后台


    js

    //index.js
    //获取应用实例
    const app = getApp()
    Page({
      bindtest: function () {
        wx.request({
          url: 'http://localhost:8080/Tally/WXServlet',
          data: {
            username: '刘子煜',
            password: '123'
          },
          method: 'POST',
          header: {
            //'content-type': 'application/json' // 默认值
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res.data);
          },
          fail: function (res) {
            console.log(".....fail.....");
          }
        })
      }
    })

    wxml

    <!--index.wxml-->
    <view class="container">
    
      <button bindtap='bindtest'>test</button>
    </view>

    Java servlet

    package controller;
    
    import java.io.IOException;
    import java.io.Writer;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.tomcat.jni.User;
    
    import com.google.gson.Gson;
    
    import bean.user;
    
    /**
     * Servlet implementation class WXServlet
     */
    @WebServlet("/WXServlet")
    public class WXServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public WXServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置请求编码
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            /* 设置响应头允许ajax跨域访问 */
            response.setHeader("Access-Control-Allow-Origin", "*");
            /* 星号表示所有的异域请求都可以接受, */
            response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    
            user user = new user();
            //获取微信小程序get的参数值并打印
            user.setName(request.getParameter("username"));
            user.setPassword(request.getParameter("password"));
            System.out.println("username="+user.getName()+" ,password="+user.getPassword());
            //转成json数据
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("data", user);
            result.put("msg", "后台已收到");
            //使用Gson类需要导入gson-2.8.0.jar
            String json = new Gson().toJson(result);
    
            //返回值给微信小程序
            Writer out = response.getWriter();
            out.write(json);
            out.flush();
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }

    注意:使用Gson类需要导入gson-2.8.0.jar

    bean:user

    package bean;
    
    public class user {
    private int id;
    private String name;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    }

  • 相关阅读:
    A survey for developing of reinforcement learning environments
    04-STM32+ESP8266+AIR202基本控制篇-功能2-微信小程序使用APUConfig配网绑定ESP8266,并通过MQTT和ESP8266实现远程通信控制
    03-STM32+ESP8266+AIR202基本控制篇-功能1-APP使用APUConfig配网绑定ESP8266,并通过MQTT和ESP8266实现远程通信控制
    02-STM32+ESP8266+AIR202基本控制篇- 基本控制篇概述
    01-STM32+ESP8266+AIR202基本控制篇-硬件使用说明
    深入学习Redis(3):主从复制(转)
    k8s常用命令
    photoshop,3dsmax,maya等图形图像动画学习资料收集
    工业机器人(ABB+Kuka)资料打包下载
    .DS_Store 是什么
  • 原文地址:https://www.cnblogs.com/a155-/p/12300720.html
Copyright © 2020-2023  润新知