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


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));
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM