开发:idea+tomcat+小程序开发平台

小程序平台:index.js页面这边添加点击绑定事件

bindtest:function(){
wx.request({
url: 'http://localhost:8080/first-webapp/testConnect',
data:{
username:'tanjingyong',
password:'abc'
},
method:'GET',
header:{
'content-type':'application/json'
},
success:function(res){
console.log(res.data);
},
fail:function(res){
console.log("something failed");
}
})
}
//需要取消开发工具的域名校验
//index.wxml 编辑view,添加页面
<!--index.wxml-->
<view>
<button bindtap='bindtest'>test</button>
</view>
二、idea上新建web项目,新建servlet提供通信
  2.1 没有HttpServlet类的问题,未绑定tomcat源码
  https://blog.csdn.net/Zrm19950822/article/details/78949616/
  2.2 添加WebServlet注解
  2.3 创建一个web项目的坑
  https://blog.csdn.net/longshehe9319/article/details/78791200
@WebServlet("/testConnect")
public class testConnect extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
/* 设置响应头允许ajax跨域访问 */
response.setHeader("Access-Control-Allow-Origin", "*");
/* 星号表示所有的异域请求都可以接受, */
response.setHeader("Access-Control-Allow-Methods", "GET,POST");

//获取微信小程序get的参数值并打印
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username="+username+" ,password="+password);

//返回值给微信小程序
Writer out = response.getWriter();
out.write("进入后台了");
out.flush();
}
运行后可以看到参数传过来
posted on 2018-07-12 17:30  SuperTan  阅读(206)  评论(0编辑  收藏  举报