How to use post for JSON instead of jQuery’s getJSON

Total
0
Shares

jQuery has a nice function called getJSON() which performs a GET requests to a url and parses the result to a JSON object. But what if you want to do a POST request instead of a GET request. This is specially useful if you have very long strings that might break the GET request.

jQuery has no postJSON function so you will have to find another way. But there is a solution!

You can use the regular post function:  jQuery.post( url, [data], [callback] , [type]) specifing the last parameter [type] to be “json”.

Here is the complete example:

var callback = function (returndata, status) {
//here you can do whatever you want with the returndata object
};

jQuery.post( url, {mydata: mydata, mydata1: mydata1}, callback, "json") ;

You can send any number of parameters to that url. The parameters are marked with italic mydata, mydata1
I hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like