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