E企盈营销工具技术服务商 热线:4006-838-530

微信小程序连接java后端

E企盈直播平台营销卖货系统

所需环境1.微信开发者工具2.eclipse小程序端Java后端运行结果注意事项不校验合法域名在详情中,这个勾勾一定要勾上,不然就会这样最后代码附上w微信端wxml:formbutton bindtap=’setcontent’发送/button/form微信端js:setcontent: function(){    wx.request({      url: ‘http://localhost:8080/servletTest/test’,      data: {        name:”tom”,      },      method: ‘get’,      header: {        ‘content-type’: ‘application/x-www-form-urlencoded’ // 默认值      },      success: function (res) {        console.log(res.data);      },      fail: function (res) {        console.log(“反馈未提交,请检查网络”);      }    })  }java后端的servlet:package servletTest;import java.io.IOException;import java.io.Writer;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class servlet02 extends HttpServlet {private static final long serialVersionUID = 1L;             protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub                response.setContentType(“text/html;charset=utf-8”);                  /* 设置响应头允许ajax跨域访问 */          response.setHeader(“Access-Control-Allow-Origin”, “*”);          /* 星号表示所有的异域请求都可以接受, */          response.setHeader(“Access-Control-Allow-Methods”, “GET,POST”);                 //获取微信小程序get的参数值并打印        String name = request.getParameter(“name”);                System.out.println(“姓名   =  “+name);                //返回值给微信小程序        Writer out = response.getWriter();         out.write(“后台已连接”);        out.flush();       }protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序连接java后端
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏