小程序_wx:request(){data}json中數(shù)組的坑

以下是一個(gè)坑:

開(kāi)始接觸小程序api的wx:request的規(guī)范是這樣的

wx.request({
  url: 'test.php', //僅為示例,并非真實(shí)的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json' // 默認(rèn)值
  },
  success: function(res) {
    console.log(res.data)
  }
})

這種寫法的參數(shù)傳輸內(nèi)容:



感覺(jué)數(shù)據(jù)不是json格式,但是content-type確實(shí)是指定了json

由于業(yè)務(wù)原因,這個(gè)時(shí)候我需要向服務(wù)器傳輸兩個(gè)數(shù)組,于是我的wx:request是這樣的:

submit:function(e){
    var param = { titles: ['col1', 'col2', 'col3'],price:[1,2] }//測(cè)試數(shù)據(jù)
    wx.request({
      method: 'POST',
      dataType: "json",
      url: '-------------------------------------------',
      data: param,
  
      success: function (res) {
        
      }
    })
  }

服務(wù)器controller:

@RequestMapping(value = "/advancedSearch",method = RequestMethod.POST)
    public ResultDto advancedSearch(@RequestParam(value = "titles[]") String[] titles,@RequestParam(value = "price[]") Integer[] price){
        System.out.println(titles);
        System.out.println(price);
        return new ResultDto("success",1);
    }

在檢查工具中發(fā)現(xiàn),400錯(cuò)誤,認(rèn)真閱讀請(qǐng)求內(nèi)容如下


image.png

個(gè)人理解是:控制器從json中自動(dòng)解析出兩個(gè)數(shù)組,但是我錯(cuò)了。。。


image.png

于是我試著把控制器中的中括號(hào)去掉,并沒(méi)有改變。

解決方式:(仍未深究為什么出錯(cuò),請(qǐng)大神指點(diǎn))

方法一:

但是我發(fā)現(xiàn),如果你的data不是json字符串格式的話,controller是能夠反序列化出數(shù)組的。

submit:function(e){
    // var param = { titles: ['col1', 'col2', 'col3'],price:[1,2] }
    wx.request({
      method: 'POST',
      dataType: "json",
      url: '------------------------------------------------------------',
      data: ['col1', 'col2', 'col3'],
      // header:{
      //   "Accept": "application/json, text/javascript, */*; q=0.01",
      //   "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
      // },
      success: function (res) {
        
      }
    })
  }
@RequestMapping(value = "/advancedSearch",method = RequestMethod.POST)
    public ResultDto advancedSearch(@RequestBody String[] titles){
        System.out.println(titles);
//        System.out.println(price);
        return new ResultDto("success",1);
    }

image.png

成功是成功了,但是沒(méi)辦法一次傳輸兩個(gè)數(shù)組,除了二維數(shù)組。
注意這里兩個(gè)controller@RequestParam與@RequestBody的不同之處

提問(wèn):為什么會(huì)返回這個(gè)錯(cuò)誤????煩死了

只要json數(shù)據(jù)是這種格式:{titles:["a","b"]}
就無(wú)法反序列化,需要在controller參數(shù)中獲取json字符串,手動(dòng)解析json
或者寫一個(gè)PO類,但是這樣只我覺(jué)得很臃腫,很不喜歡,雖然對(duì)前端人員不是很友好。

方法二:

于是我自己寫了一個(gè)html,用ajax做了一次實(shí)驗(yàn),發(fā)現(xiàn)wx:request需要修改兩個(gè)地方:

header:{
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
      },

wx:request 默認(rèn) content-type:application/json
修改之后的代碼:

submit:function(e){
    var param = { titles: ['col1', 'col2', 'col3'],price:[1,2] }
    wx.request({
      method: 'POST',
      dataType: "json",
      url: '-----------------------------------------------------------------------',
      data: param,
      header:{
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
      },
      success: function (res) {
        
      }
    })
  }
@RequestMapping(value = "/advancedSearch",method = RequestMethod.POST)
    public ResultDto advancedSearch(@RequestParam(value = "titles") String[] titles,@RequestParam(value = "price") Integer[] price){
        System.out.println(titles);
        System.out.println(price);
        return new ResultDto("success",1);
    }

注意:這時(shí)候的請(qǐng)求信息改變了:


image.png

之前報(bào)錯(cuò)的版本:


image.png

個(gè)人理解:

方法一之所以行得通,是因?yàn)閭鬏數(shù)臄?shù)據(jù)不是json格式(如下,我認(rèn)為它不是json)

image.png

當(dāng)傳輸?shù)臄?shù)據(jù)為json字符串時(shí),會(huì)返回反序列化錯(cuò)誤:
JSON parse error: Cannot deserialize instance of java.lang.String[] out of

補(bǔ)充測(cè)試用的ajax

<html>
    <head>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("#b01").click(function(){

        var param = {titles:['col1','col2','col3']}; 
        htmlobj=$.ajax({url:"----------------------------------------------", 
        type:"post", 
        data:param, 
        async:false, 
        dataType:"json",/**這個(gè)類型很重要 */ cache:false, error:function (data) { 
        rtnObj = data.responseText; 
    }, beforeSend:function (XMLHttpRequest) { 
        //ajaxStart(); 
    }, complete:function (XMLHttpRequest, textStatus) { 
        //ajaxComplete(); 
    }, success:function (data) { 
        rtnObj = data; 
    }});

      $("#myDiv").html(htmlobj.responseText);
      });
    });
    </script>
    </head>
    <body>
    
    <div id="myDiv"><h2>通過(guò) AJAX 改變文本</h2></div>
    <button id="b01" type="button">改變內(nèi)容</button>
    
    </body>
    </html>
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評(píng)論 19 139
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,203評(píng)論 8 265
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,931評(píng)論 1 92
  • 生活不會(huì)只有歡喜,郁悶有時(shí)也會(huì)趁虛而入,讓人猝不及防,無(wú)所適從。 月有陰晴圓缺,人有悲歡離合,這是自然法則,誰(shuí)能逃...
    醉舞飛洋閱讀 253評(píng)論 0 1
  • 經(jīng)過(guò)兩小時(shí)車程,今天來(lái)到了Muresk機(jī)構(gòu)——澳大利亞內(nèi)陸確實(shí)比沿海冷,颼颼的確實(shí)涼意滲人。不方便的是,4G網(wǎng)變得...
    何如一醉盡忘機(jī)閱讀 147評(píng)論 0 0

友情鏈接更多精彩內(nèi)容