I know this question has been asked quite a lot before, but I spent more than 4 hours trying to figure out what is the problem but I didn't have any luck to find a similar case. I have complex javascript object that I need to convert it to Json using Json.stringfy() method, but the problem is I'm always getting empty Object. Here is a screenshot for the object, and this is the result I'm getting after calling json.stringfy().
Here is the code for creation the object:
function Step2() {
debugger;
//Get all table rows
var tableRows = $('#ProjectsTable > tbody > tr');
//Remove first two rows
tableRows.splice(0, 2);
var tds = []
tableRows.each(function () {
tds.push($(this).find('td'));
})
var self = new Object();
self.Projects = new Array();
for (var i = 0; i < tds.length; i++) {
self.Projects.push([]);
self.Projects[i].Years = new Array();
self.Projects[i].ProjectName = "";
self.Projects[i].ProjectName = $(tds[i][0]).find('span').text();
for (var j = 1; j < tds[i].length;) {
var Year = new Object();
Year.TimeAllocated = $(tds[i][j++]).text();
Year.Duration = $(tds[i][j++]).text();
Year.Cost = $(tds[i][j++]).text();
self.Projects[i].Years.push(Year);
}
}
return self;
}
function SubmitStep2() {
debugger;
var request = Step2();
var jsonData = JSON.stringify(request);
debugger;
$.ajax({
url: "/ResearchGrant/ProjectManpower",
dataType: 'json',
contentType: "application/json",
type: "POST",
data: jsonData ,
})
.success(function (result) {
ShowSuccessMessage();
})
.error(function (xhr, status) {
alert(status + xhr.error);
})
}