博客
关于我
js,小程序共用java后端进行数据传输
阅读量:306 次
发布时间:2019-03-04

本文共 2429 字,大约阅读时间需要 8 分钟。

一,java后端代码
package maven_weather;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;/** * Servlet implementation class testServelt */public class testServelt extends HttpServlet {	private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public testServelt() {        super();        // TODO Auto-generated constructor stub    }	/**	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)	 */	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		System.out.println("接受请求成功!");		System.out.println("定义一个数组,存放数据!");				String callbacka=request.getParameter("callbackfun");		System.out.println("回调函数的名字:"+callbacka);		//		这里自己定义一个数组,提供测试		ArrayList
peopleList=new ArrayList
(); peopleList.add("1230"); peopleList.add("189"); peopleList.add("456"); peopleList.add("0000");// System.out.print("遍历这个peopelList数组:");// for(String l:peopleList){// System.out.print(l+" ");// }// System.out.println();// System.out.println("========================="); // 将数组转换成json格式,然后再交给回调函数, System.out.println("将数组转换成json格式:"); JSONArray jArray = JSONArray.fromObject(peopleList); System.out.println(jArray); PrintWriter writer=response.getWriter(); String s = "{\"images\": "+jArray+"}"; if(callbacka==null){ s=s; }else{ s=callbacka+"("+s+")"; } response.getWriter().write(s); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}

二,html代码

			
三,微信小程序js代码

/*  向后端发送请求  */  btnPost:function(){    var url="http://localhost:8080/maven/testServelt"    wx.request({      url: url,      // jsonp: 'callbackfun',//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)      // jsonpCallback: "jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名      headers: {        'Content-Type': 'application/json'      },      success: function (res) {        console.log(res.data.images);      }    })  }

转载地址:http://drjq.baihongyu.com/

你可能感兴趣的文章
[编程题]:n头牛中选择满足所有m种特性的牛(百度2021)
查看>>
Redis中的删除策略和逐出算法
查看>>
[数据结构]:红黑树(二)
查看>>
PyCharm安装与配置
查看>>
Docker - 部署 Redis 6.0.8
查看>>
Android - Create dynamic lists with RecyclerView
查看>>
Android - Broadcasts overview(不完整)
查看>>
Spring Boot - Add a Servlet to an Application
查看>>
OrCAD Capture CIS 16.6 - 为元器件添加属性
查看>>
Web API - File - Selecting files using drag and drop
查看>>
Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
查看>>
排序算法总结——c++实现
查看>>
vector的几种初始化及赋值方式
查看>>
STL基础梳理 2019.1.19(仿函数,谓词,内建函数对象,适配器,算法)
查看>>
IDEA效率快捷键收集
查看>>
并发情况下三种线程/并发安全
查看>>
希尔排序
查看>>
TreeView 中 SelectedNodeStyle 无效,selectedClass 无效的参考方案
查看>>
C#,asp.net,ashx处理session
查看>>
501 5.1.7 Invalid address
查看>>