简体   繁体   English

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

[英]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) 我的前端是javascript,后端是Java(框架春季)

I try to post an json request to java controller but i've got an error: 我尝试将json请求发布到Java控制器,但出现错误:

"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;" “ bean类[...]的无效属性'toto [tata]':在索引属性路径'toto [tata]'中引用的属性既不是数组,也不是列表,也不是映射;”

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: 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) {
               ...
      }
}

Anyone could help me? 有人可以帮助我吗? :-) :-)

Thanks 谢谢

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初始化为{}然后向其中添加属性tata

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

When you create var newData = Object(); 创建var newData = Object(); now newData is plain empty object {} . 现在newData是普通的空对象{} Then you are trying to add a property tata to a non-existing property toto inside newData object (as it is empty). 然后,您尝试添加属性tata到不存在的财产toto newData对象内(因为它是空的)。 So you need to create a property (object) toto inside the empty object newData in order to add a property (string) tata to it. 因此,您需要在空对象newData内创建一个属性(对象) toto ,以便向其添加属性(字符串) tata

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM