Post json request object with object as a member

问题: I've got a problem. My front is in javascript and my backend in java (framework spring) I try to post an json request to java controller but i've got an error: "I...

问题:

I've got a problem.

My front is in javascript and my backend in java (framework spring)

I try to post an json request to java controller but i've got an error:

"Invalid property 'toto[tata]' of bean class [...]: Property referenced in indexed property path 'toto[tata]' is neither an array nor a List nor a Map;"

My class to wrap:

Class Test {
   Toto toto;
   String var1;
}

Class Toto {
   String tata;
}

@RequestMapping(..., method = RequestMethod.POST)
@ResponseBody
public jsonresponse testFunction(Test testrequest) { ... }

Javascript side:

ajax: {
      "url": '/atscomhmi-web/servlet/archive/audio',
      "type": "POST",
      data: function (data) { 
          var newData = Object();
          newData['var1'] = "it runs"
          newData.toto[tata] = "it doesn't work"
          return newData;
      },
      "dataSrc": function (returnedDataFromBackend) {
               ...
      }
}

Anyone could help me? :-)

Thanks


回答1:

replace

newData.toto[tata] = "it doesn't work"

to be:

newData = {
    "var1": "it runs",
    "toto": {
        "tata": "it doesn't work" // this string can be any value (data.variable1)
    }
};

alternatively you can first initialize the property newData.toto into {} then add property tata to it:

newData.toto = {};
newData.toto.tata = "it doesn't work"; // or any value you want (data.variable1)

When you create var newData = Object(); now newData is plain empty object {}. Then you are trying to add a property tata to a non-existing property toto inside newData object (as it is empty). So you need to create a property (object) toto inside the empty object newData in order to add a property (string) tata to it.

  • 发表于 2018-12-30 11:49
  • 阅读 ( 295 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除