title: Setting the POST-body to a JSON object with jQuery
body: <p>I'm trying to write a JSON-based web API in a Sinatra app. I want to POST a JSON object as the post body (with the proper content-type set) but I'm struggling.</p>
  
  <p>In Cocoa, I'd do something like</p>
  
  <pre><code>[mutableHTTPRequest setHTTPBody:dataRepresentationOfJSONObject];
  </code></pre>
  
  <p>And the content type, set to JSON, would then post the HTTP body as a JSON object. I'm trying to do this with jquery. The best I can do so far just takes the JSON object and turns it into a normal style <code>key=value&amp;…</code> style post body, and that's <em>not</em> what I'm after.</p>
  
  <p>My Javascript:</p>
  
  <pre><code>var data = { "user" : "me!" };
  $.ajax({
      type: "POST",
      url: "/api/user/create",
      contentType: 'application/json',
      data: data,
      success: function(r) {
  
  });
  </code></pre>
  
  <p>Any pointers on how to do this? My goal is for my Sinatra to do like the following</p>
  
  <pre><code>post "/api/user/create" do
      js = JSON.parse(request.body.read)
      # do something with the js object… this works when POSTing from Cocoa
  end
  </code></pre>