简体   繁体   中英

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:

"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": [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 = {};
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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