Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a form with fields and a list of another object with items. But with jQuery
$("form").serializeObject()
, it is not creating the right object like here:
var functionViewModel = new Object();
functionViewModel.Id = 1;
functionViewModel.Name = "F-I-001";
functionViewModel.Localised = [];
functionViewModel.Localised.push({
LocalisedId: '1|1',
Subject: "F-N-001"
functionViewModel.CategoryViewModel = new Object();
functionViewModel.CategoryViewModel.CategoryInGroup = [];
functionViewModel.CategoryViewModel.CategoryInGroup.push({
Id: '1'
Any ideas?
var recEncoded = $.param( myObj);
var recDecoded = decodeURIComponent( $.param( myObj ) );
alert( recEncoded );
–
Before sending data to server you need to stringify()
the object. At the server you need to parse()
it.
Use JSON.stringify()
to convert object to string. Use
JSON.parse()
to convert JSON object from string.
var functionViewModel = new Object();
functionViewModel.Id = 1;
functionViewModel.Name = "F-I-001";
functionViewModel.Localised = [];
functionViewModel.Localised.push({
LocalisedId: '1|1',
Subject: "F-N-001"
functionViewModel.CategoryViewModel = new Object();
functionViewModel.CategoryViewModel.CategoryInGroup = [];
functionViewModel.CategoryViewModel.CategoryInGroup.push({
Id: '1'
var str = JSON.stringify(functionViewModel)
var obj = JSON.parse(str);
console.log(str);
console.log(obj);
$.post(
url: url, //Url of your server
data: JSON.stringify(obj), //data you want to send to server
function(data, status) {
alert("Success");
As pointed by Heena, you can use param()
You need to serialize/convert the object to a string before submitting it. You can use $.param()
for this.
$('#form').val($.param(functionViewModel));
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.