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