title: JavaScript - Building JSON object
body: <p>I'm trying to understand how to build a JSON object in JavaScript. This JSON object will get passed to a JQuery ajax call. Currently, I'm hard-coding my JSON and making my JQuery call as shown here:</p>
  
  <pre><code>$.ajax({
    url: "/services/myService.svc/PostComment",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: '{"comments":"test","priority":"1"}',
    dataType: "json",
    success: function (res) {
      alert("Thank you!");
    },
    error: function (req, msg, obj) {
      alert("There was an error");
    }
  });        
  </code></pre>
  
  <p>This approach works. But, I need to dynamically build my JSON and pass it onto the JQuery call. However, I cannot figure out how to dynamically build the JSON object. Currently, I'm trying the following without any luck:            </p>
  
  <pre><code>var comments = $("#commentText").val();
  var priority = $("#priority").val();
  var json = { "comments":comments,"priority":priority };
  
  $.ajax({
    url: "/services/myService.svc/PostComment",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: json,
    dataType: "json",
    success: function (res) {
      alert("Thank you!");
    },
    error: function (req, msg, obj) {
      alert("There was an error");
    }
  }); 
  </code></pre>
  
  <p>Can someone please tell me what I am doing wrong? I noticed that with the second version, my service is not even getting reached. </p>
  
  <p>Thank you</p>