Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

$.postJSON #28

Closed
Krinkle opened this issue May 23, 2014 · 3 comments
Closed

$.postJSON #28

Krinkle opened this issue May 23, 2014 · 3 comments

Comments

@Krinkle
Copy link
Owner

Krinkle commented May 23, 2014

From benjamin.schweizer on October 18, 2009 00:40:39

/*
 * Hello Brandley, tonight I've tried to figure out how to do proper
 * JSON POSTs using "Content-type: application/json" and serialized JSON
 * data in the content portion (body) of my HTTP requests. This is
 * specified in the Twitter API for status updates and Identica JSON
 * webservices (found via Mark Pilgrim).
 * After some tests, I had to recognize that jQuery does not support JSON
 * encoding in the core distribution and aside of $.getJSON(), there
 * is no $.postJSON().
 * Below is an proposed update. As it relies on your json plugin, I'd ask
 * you to add it to your code base so that other jQuery users can benefit
 * of it.
 */

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
        'type': 'POST',
        'url': url,
        'contentType': 'application/json',
        'data': $.toJSON(data),
        'dataType': 'json',
        'success': callback
    });
};

Original issue: http://code.google.com/p/jquery-json/issues/detail?id=28

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From coolaj86 on January 21, 2010 12:48:47

There's a jquery.cors plugin which I'm working on which also relies on jquery.json
and I have that method in there.

If you'd like to help, let me know.

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From krinklemail on September 17, 2011 18:08:19

I'm not sure what the purpose of this function is intended to be.

jQuery has $.getJSON, which takes (url, data, success) and it makes a GET request to 'url' with data (which is an object, translated to a querystring like foo=bar&lorem=ipsum) and it parses the return value of the server (not the sending value of the client) as JSON and passes the parsed object into the success callback function.

A function named postJSON, in my opinion, would make a similar request but as POST, parsing the server return as JSON, not sending JSON to the server.

This seems a bit out of the scope of this plugin in my opinion. You could request the following to be added to jQuery though, in which case you can send JSON to the server in POST like this:

  // in jQuery core
  $.postJSON = function( url, data, callback ) {
    return jQuery.get( url, data, callback, "json" );
  }

  // In your application
  $.postJSON( 'awesome.php', { data: $.toJSON( ... ), function( response ) { ... } );

Status: WontFix

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From benjamin.schweizer on September 18, 2011 03:09:38

From the reference implementation ( http://json.org/JSONRequest.html ):
"JSONRequest.post does an HTTP POST of a the serialization of a JavaScript object or array, gets the response, and parses the response into a JavaScript value. If the parse is successful, it returns the value to the requesting script. In making the request, no HTTP authentication or cookies are sent. Any cookies returned by the server cause the request to fail. The JSONRequest service can only be used to send and receive JSON-encoded values. JSONRequest cannot be used to retrieve other text formats."

From there, the proposed $.postJSON() function looks consistent to me, but it depends on a 3rd party library like jquery-json (or json2.js) to do the json encoding.

@Krinkle Krinkle closed this as completed May 23, 2014
@Krinkle Krinkle added enhancement and removed bug labels Jul 3, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant