views:

121

answers:

3

Hello,

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:

$.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");
  }
});        

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:

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");
  }
}); 

Can someone please tell me what I am doing wrong? I noticed that with the second version, my service is not even getting reached.

Thank you

+2  A: 

You may want to look at the JSON JavaScript library. It has a stringify() function which I think will do exactly what you need.

LBushkin
That's what I thought initially, but from the code he is using in his example, jQuery is supposed to take care of this for you.
Justin Ethier
@Justin Ethier: no, jQuery has no JSON serialization (only deserialization). jQuery does something completely different with the data object @user208662 is passing to it.
Crescent Fresh
@Crescent Fresh: the docs (http://api.jquery.com/jQuery.ajax/) have an example of doing `data: ({id : this.getAttribute('id')})`
Mark
Exactly. I use the same syntax in my application and it works just fine without `stringify` (which is still useful elsewhere, of course).
Justin Ethier
@Mark, @Justin Ethier: again, jQuery does not use JSON serialization. The data object passed in @Mark's example and answer simply gets routed to `jQuery.param()` (http://api.jquery.com/jQuery.param/), a different kind of serialization altogether.
Crescent Fresh
@Crescent Fresh: I see. One passes the entire string, which happens to be a JSON object, and the other will create a set of name/value parameters from the JSON object you give it.
Mark
A: 

This should work

var json = { comments: "comments",priority: "priority" };
Rigobert Song
As far as I understand it, in JSON all strings need to be double quoted, so "comments" and "priority". That is a Javascript object, so if you were trying to show just a Javascript object, it is valid. For the JSON format, though, strings need double quotes.
justkt
Yeah your right, just meant to remove them from the property
Rigobert Song
A: 

Remove the quotes

data: '{"comments":"test","priority":"1"}',

becomes

data: {"comments":"test","priority":"1"},

JSONs are objects not strings.

plodder