9

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

5
  • 1
    Do you get any Javascript errors? Commented Mar 26, 2010 at 15:32
  • Excellent point - try running this with the Firebug debugger turned on and see what you get. Commented Mar 26, 2010 at 15:54
  • I am not getting any JavaScript errors. The error event handler in my $.ajax call is getting triggered though. Commented Mar 26, 2010 at 16:30
  • Well there you go... what error message are you getting back? Is it helpful? Commented Mar 26, 2010 at 17:00
  • Doh! It was a type mismatch issue. Commented Mar 26, 2010 at 19:03

5 Answers 5

8

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

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

6 Comments

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: no, jQuery has no JSON serialization (only deserialization). jQuery does something completely different with the data object @user208662 is passing to it.
@Crescent Fresh: the docs (api.jquery.com/jQuery.ajax) have an example of doing data: ({id : this.getAttribute('id')})
Exactly. I use the same syntax in my application and it works just fine without stringify (which is still useful elsewhere, of course).
@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() (api.jquery.com/jQuery.param), a different kind of serialization altogether.
|
6

Your code:

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { "comments":comments,"priority":priority };

Take out the quotes ( line 3 ):

var comments = $("#commentText").val();
var priority = $("#priority").val();
var json = { comments: comments, priority: priority };

Comments

3

Remove the quotes

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

becomes

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

JSONs are objects not strings.

Comments

2

This should work

var json = { comments: "comments",priority: "priority" };

1 Comment

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.
2

this works for me.

var json = "{ 'comments': '" + *comments* +"','priority:' '" + *priority* +"' }";

italics are the variables.

1 Comment

What happens if the comments and priority variables also includes double quotes? This fails. Not the ideal solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.