微信小程序 与后台交互----传递和回传时间

wxml代码

<!--index.wxml-->
<view class="container">
<view class="section">
  <view class="section__title">日期选择器</view>
  <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
    <view class="picker">
      当前选择: {{date}}
    </view>
  </picker>
  <button id="btn" bindtap="abc" >提交</button>
  <view>返回的值:<text>{{date2}}</text></view>
</view>
</view>

index.js代码

  abc: function (e) {//该函数用于和后台交互
    var that = this;
    wx.request({
      url: 'https://www.kjch.xyz/data/Data', //仅为示例,并非真实的接口地址
      data: {
        date:this.date,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
       console.log(res.data)
        //var date = new Date(res.data);
        //var abc = format.formatTime(res.data.expirationDate, 'Y/M/D');
        console.log(res.data); 
        //if(this.data!=""){
          that.setData({
            date2: res.data
          })
       // }
      }
    })
  },

 

JAVA代码

		// 跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		String date = request.getParameter("date");//接受传入的字符串类型日期
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");格式化日期类型
		System.out.println(date);
		
          //如果传入的值为空则新建一个Date对象 if (date == "undefined" || date == null || date == "") { date = sdf.format(new Date()); } JsonUtil.printJson(response, date);

  

JAVA 转Json代码

1
2
3
4
5
6
7
8
public static void printJson(HttpServletResponse response, Object obj) throws IOException {
    // 返回数据,返回数据类型是json
    response.setContentType("application/json");
    // 返回数据编码UTF-8
    response.setCharacterEncoding("UTF-8");
    // 返回数据,通过gson将数据返回给Ajax 通过gson工具提高工作效率
    response.getWriter().write(new Gson().toJson(obj));
}
原文地址:https://www.cnblogs.com/max-hou/p/11686093.html