Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  113] [ 5]  / answers: 1 / hits: 11104  / 10 Years ago, thu, january 15, 2015, 12:00:00

I am working on sending JSON, via jQuery ajax, to a Node server. My jQuery ajax is working. See below.



var user = JSON.stringify(userObject);
$.ajax({
type: 'POST',
url: 'http://localhost:5000/save',
contentType: 'application/json',
dataType: 'json',
data: user
})
.done(function(data) {
console.log(data.link, 'here is the link');
callback(data.link);
})
.fail(function(err) {
console.log(err);
callback(err);
});


My issue is that when I console log user, a json object that has been stringified, I have information inside arrays that are being lost. The part of the object I am losing looks like this.



Losing



And it is showing up stringified in the console like this:



stringified



The information that is stored inside of those arrays inside of the parent user object are not being stored. Any suggestion to why this might be will help. If you have any alternative methods that I can use to send this data structure via jQuery ajax, please let me know.



Edit
Here is where regions is created:



// Add regions to the bracket layout object
user.regions = [];
for (var a = 0; a < 2; a++) {
user.regions[a] = [];
user.regions[a].columns = [];
}


Thanks,


More From » jquery

 Answers
31

Well, the problem is that you're creating AN ARRAY then continue working with it as with an object.



Use



user.regions[a] = {};


instead.



What happens is that JSON.stringify sees there is an array, tries to iterate over its numeric indexes which it does not have so it results in an empty array.



Example on JSFiddle: http://jsfiddle.net/Le80jdsj/


[#39991] Wednesday, January 14, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leannjaidynd

Total Points: 111
Total Questions: 100
Total Answers: 94

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;