微信小程序 與后台交互----傳遞和回傳時間


wxml代碼

<!--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代碼

  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代碼

		// 跨域
		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));
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM