百度

微信小程序 与后台交互----获取服务器时间

index.wxml代码

<!--index.wxml-->
<view class="container">
    <text>{{date}}</text>
    <button bindtap="abc">取得时间</button>
</view>

index.js代码

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

        that.setData({
          date: res.data
        })
      }
    })
  },

JAVA代码

		Date day = new Date();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		String s = sdf.format(day);
		
		JsonUtil.printJson(response,s);

  

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));
}
posted @ 2019-10-16 16:19  麦克斯-侯  阅读(1881)  评论(0)    收藏  举报
百度