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

index.wxml代码javascript

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

index.js代码java

  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代码json

		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代码app

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));
}
相关文章
相关标签/搜索