{"name":"jQuery.post","type":"method","title":"jQuery.post()","deprecated":null,"removed":null,"desc":"Load data from the server using a HTTP POST request.","categories":["ajax/shorthand-methods","version/1.0","version/1.5"],"entries":[{"return":"jqXHR","signatures":{"added":"1.0","argument":[{"desc":"A string containing the URL to which the request is sent.","name":"url","type":"String"},{"type":[{"name":"PlainObject"},{"name":"String"}],"desc":"A plain object or string that is sent to the server with the request.","name":"data","optional":"true"},{"desc":"A callback function that is executed if the request succeeds. Required if dataType is provided, but can be null in that case.","name":"success(data, textStatus, jqXHR)","optional":"true","type":"Function"},{"desc":"The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).","name":"dataType","optional":"true","type":"String"}]},"examples":[{"desc":"Request the test.php page, but ignore the return results.","code":"\n$.post( \"test.php\" );\n"},{"desc":"Request the test.php page and send some additional data along (while still ignoring the return results).","code":"\n$.post( \"test.php\", { name: \"John\", time: \"2pm\" } );\n"},{"desc":"Pass arrays of data to the server (while still ignoring the return results).","code":"\n$.post( \"test.php\", { 'choices[]': [ \"Jon\", \"Susan\" ] } );\n"},{"desc":"Send form data using ajax requests","code":"\n$.post( \"test.php\", $( \"#testform\" ).serialize() );\n"},{"desc":"Alert the results from requesting test.php (HTML or XML, depending on what was returned).","code":"\n$.post( \"test.php\", function( data ) {\n alert( \"Data Loaded: \" + data );\n});\n"},{"desc":"Alert the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).","code":"\n$.post( \"test.php\", { name: \"John\", time: \"2pm\" })\n .done(function( data ) {\n alert( \"Data Loaded: \" + data );\n });\n"},{"desc":"Post to the test.php page and get content which has been returned in json format (\"John\",\"time\"=>\"2pm\")); ?>).","code":"\n$.post( \"test.php\", { func: \"getNameAndTime\" }, function( data ) {\n console.log( data.name ); // John\n console.log( data.time ); // 2pm\n}, \"json\");\n"},{"desc":"Post a form using ajax and put results in a div","code":"\n// Attach a submit handler to the form\n$( \"#searchForm\" ).submit(function( event ) {\n\n // Stop form from submitting normally\n event.preventDefault();\n\n // Get some values from elements on the page:\n var $form = $( this ),\n term = $form.find( \"input[name='s']\" ).val(),\n url = $form.attr( \"action\" );\n\n // Send the data using post\n var posting = $.post( url, { s: term } );\n\n // Put the results in a div\n posting.done(function( data ) {\n var content = $( data ).find( \"#content\" );\n $( \"#result\" ).empty().append( content );\n });\n});\n","html":"\n
\n \n \n
\n\n
\n"}],"longdesc":"\n

This is a shorthand Ajax function, which is equivalent to:

\n
\n$.ajax({\n  type: \"POST\",\n  url: url,\n  data: data,\n  success: success,\n  dataType: dataType\n});\n    
\n

The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.

\n

As of jQuery 1.5, the success callback function is also passed a \"jqXHR\" object (in jQuery 1.4, it was passed the XMLHttpRequest object).

\n

Most implementations will specify a success handler:

\n
\n$.post( \"ajax/test.html\", function( data ) {\n  $( \".result\" ).html( data );\n});\n    
\n

This example fetches the requested HTML snippet and inserts it on the page.

\n

Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

\n

The jqXHR Object

\n

As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or \"jqXHR,\" returned by $.get() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise (see Deferred object for more information). The jqXHR.done() (for success), jqXHR.fail() (for error), and jqXHR.always() (for completion, whether success or error) methods take a function argument that is called when the request terminates. For information about the arguments this function receives, see the jqXHR Object section of the $.ajax() documentation.

\n

The Promise interface also allows jQuery's Ajax methods, including $.get(), to chain multiple .done(), .fail(), and .always() callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.

\n
\n// Assign handlers immediately after making the request,\n// and remember the jqxhr object for this request\nvar jqxhr = $.post( \"example.php\", function() {\n  alert( \"success\" );\n})\n  .done(function() {\n    alert( \"second success\" );\n  })\n  .fail(function() {\n    alert( \"error\" );\n  })\n  .always(function() {\n    alert( \"finished\" );\n});\n\n// Perform other work here ...\n\n// Set another completion function for the request above\njqxhr.always(function() {\n  alert( \"second finished\" );\n});\n    
\n\n

Deprecation Notice

\n

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

\n "}]}