wxml代碼javascript
<!--index.wxml--> <view class="container"> <view class="section"> <view class="section__title">日期選擇器</view> <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange"> <view class="picker"> 當前選擇: {{date}} </view> </picker> <button id="btn" bindtap="abc" >提交</button> <view>返回的值:<text>{{date2}}</text></view> </view> </view>
index.js代碼java
abc: function (e) {//該函數用於和後臺交互 var that = this; wx.request({ url: 'https://www.kjch.xyz/data/Data', //僅爲示例,並不是真實的接口地址 data: { date:this.date, }, header: { 'content-type': 'application/json' // 默認值 }, success(res) { console.log(res.data) //var date = new Date(res.data); //var abc = format.formatTime(res.data.expirationDate, 'Y/M/D'); console.log(res.data); //if(this.data!=""){ that.setData({ date2: res.data }) // } } }) },
JAVA代碼json
// 跨域 response.setHeader("Access-Control-Allow-Origin", "*"); String date = request.getParameter("date");//接受傳入的字符串類型日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");格式化日期類型 System.out.println(date);
//若是傳入的值爲空則新建一個Date對象 if (date == "undefined" || date == null || date == "") { date = sdf.format(new Date()); } JsonUtil.printJson(response, date);
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));
}
|