1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with Json.stringify

Discussion in 'JavaScript' started by Mr_Skarner, Nov 8, 2019.

  1. #1
    Hi Everyone i'm Rick 22 years and I'm new here.

    i got a problem I do not fully understand it what i have to do. can someone help me with this?

    what i have to do is this:
    Never create JSON strings manually, use JSON.stringify for this.

    and he is talking about this code:

    function saveState() {
        submitData('[{"lane 1": ' + getLaneState('lane1') + ',"lane 2": ' + getLaneState('lane2') + ',"lane 3": ' + getLaneState('lane3') + ',"lane 4": ' + getLaneState('lane4') + '}]');
    }
    
    Code (markup):

    is this what i have to do? :

    function saveState() {
    
    
        var myJSON = JSON.stringify(obj);
    
        var obj = submitData('[{"lane 1": ' + getLaneState('lane1') + ',"lane 2": ' + getLaneState('lane2') + ',"lane 3": ' + getLaneState('lane3') + ',"lane 4": ' + getLaneState('lane4') + '}]');
    }
    
    Code (markup):
     
    Mr_Skarner, Nov 8, 2019 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hello Rick :)
    I think you have to explain more. Assuming what the submitData() function does is to return a JSON object, then yes you may execute
    var myJSON = JSON.stringify(obj);
    Code (JavaScript):
    AFTER submitData() line but not before it.
     
    hdewantara, Nov 8, 2019 IP
  3. Mr_Skarner

    Mr_Skarner Banned

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    its a function

    function submitData(data) {
        var xhr = new XMLHttpRequest();
        var url = "savestate.php";
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText);
            }
        };
        xhr.send(data);
    }
    
    
    
    function saveState() {
    
    
        var myJSON = JSON.stringify(obj);
    
        var obj = submitData('[{"lane 1": ' + getLaneState('lane1') + ',"lane 2": ' + getLaneState('lane2') + ',"lane 3": ' + getLaneState('lane3') + ',"lane 4": ' + getLaneState('lane4') + '}]');
    }
    
    Code (markup):
     
    Mr_Skarner, Nov 8, 2019 IP