以对象作为成员发布json请求对象

时间:2018-12-28 16:07:52

标签: javascript java json spring

我有问题。

我的前端是javascript,后端是Java(春季框架)

我尝试将json请求发布到java控制器,但出现错误:

  

“ Bean类[...]的无效属性'toto [tata]':属性   在索引属性路径“ toto [tata]”中引用的数组都不是   既不是列表也不是地图;”

我要包装的课:

Class Test {
   Toto toto;
   String var1;
}

Class Toto {
   String tata;
}

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

JavaScript端:

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

有人可以帮助我吗? :-)

谢谢

1 个答案:

答案 0 :(得分:0)

替换

['Futrelle', 'Mrs', 'Jacques', 'Heath', 'Lily May Peel']

成为:

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

或者,您可以首先将属性newData = { "var1": "it runs", "toto": { "tata": "it doesn't work" // this string can be any value (data.variable1) } }; 初始化为newData.toto,然后向其中添加属性{}

tata

现在创建newData.toto = {}; newData.toto.tata = "it doesn't work"; // or any value you want (data.variable1) 时,newData是普通的空对象var newData = Object();。然后,您尝试将属性{}添加到newData对象内的不存在的属性tata中(因为它为空)。因此,您需要在空对象toto内创建属性(对象)toto,以便向其中添加属性(字符串)newData