Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on 

JSON.stringify problems

In the code below, I am trying to send a JSON encoded javascript object.

However, when the object is json "stringified", it removes the eventcode, price and WaitingList values.
In the JSON result (json_data), all I see is the firstname, lastname, email for each particpant that was added.

How do I prevent this from happening. The eventcode, price and WaitingList values are global for all participants.
    window.particpants = [];
    window.participants['eventcode']   = "GEVENT";
    window.participants['price']       = 99.00;
    window.participants['WaitingList'] = "";

    if(waiting_list_flag) {
      window.participants['WaitingList'] = "YES";
    } else {
      window.participants['WaitingList'] = "NO";
    }

    $('#next_btn').click(function() {
      addParticipant(); // adds the last entered participant info to the participants object
      var url = AJAX_URL_ADD
      json_data = JSON.stringify(window.participants); // json encode the participants

      $.ajax({async: false, 
              type:'POST',
              url: url, 
              data: json_data, 
              dataType: 'json', 
              contentType: "application/json; charset=utf-8", 
              success: function(data){
                if(data.result=='success') {
                  window.location = GOTO_URL;
                } else {
                  //display error
                  $('.error_message').html(data.message);
                } // if(data.result=='success')
              } // success function()
      }); // $.ajax
    }); // $('#next_btn').click

  // also called from another button click to add more participants
  function addParticipant() {
    var participant = {};
    participant.firstname   = $('#FirstName').val();
    participant.lastname    = $('#LastName').val();
    participant.email       = $('#Email').val();
    window.participants.push(participant);
  } // function addParticipant()

Open in new window

JavaScript

Avatar of undefined
Last Comment
Eddie Shipman
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

ASKER

window.participants.participants is undefined

Open in new window

If I leave it at
window.participants.push(participant)

Open in new window

I get
window.participants.push is not a function

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

did you made a copy/cut? (check line 5)

work fine here : http://jsfiddle.net/38jAn/1/
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

ASKER

Got it, missed that one. Also, modifying to this worked, is this better or should I do it like you did in the Fiddle?
    window.participants.eventcode   = "GEVENT";
    window.participants.price       = 99.00;
    window.participants.WaitingList = "";

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

no problem with this syntax, if you like it adopt it.

forexample["this-is-not-possible-with-the-other-syntax"] = 10; // you can't : forexample.this-is-not-possible-with-this-syntax = 10;

Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

ASKER

Got it, thanks.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo