7

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 this part

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

stringified console log

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,

18
  • 1
    Why do you JSON.stringify at first place? Commented Jan 15, 2015 at 0:50
  • 5
    The console pic shows that both the Arrays are empty arrays, with zero elements. Commented Jan 15, 2015 at 0:54
  • 1
    none of this is making sense. Please update question with full details Commented Jan 15, 2015 at 0:55
  • 1
    What is on the screenshot? How and where did you get it from? Commented Jan 15, 2015 at 0:59
  • 1
    keep showing us regions...but data that is being sent is user ... I voted to close since you just aren't connecting any dots. Put a proper prblem description together Commented Jan 15, 2015 at 1:02

2 Answers 2

15

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/

Sign up to request clarification or add additional context in comments.

1 Comment

I thought that might be the issue. Ok! Thank you for your time and patience!
0

I came here with the same issue of seemingly losing data with JSON.stringify. Although when I would console.Log() I'd see the data existing.

PSA Let's just all agree to remember to make sure synchronous logic isn't in async functions 🤦‍♂️

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.