You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
/* * 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){returnjQuery.ajax({'type': 'POST','url': url,'contentType': 'application/json','data': $.toJSON(data),'dataType': 'json','success': callback});};
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){returnjQuery.get(url,data,callback,"json");}// In your application$.postJSON('awesome.php',{data: $.toJSON( ... ),function(response){ ... });
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.
From benjamin.schweizer on October 18, 2009 00:40:39
Original issue: http://code.google.com/p/jquery-json/issues/detail?id=28
The text was updated successfully, but these errors were encountered: