Let's say I want to execute a PHP script. Which way is better?
This:
$.ajax({ type: "GET", url: "php-script.php", dataType: "script" });
Or this:
$.get("php-script.php", function(data) { });
get() executes an Ajax GET request. The returned data (which can be any data) will be passed to your callback handler. $(selector). load() will execute an Ajax GET request and will set the content of the selected returned data (which should be either text or HTML).
post() methods provide simple tools to send and retrieve data asynchronously from a web server. Both the methods are pretty much identical, apart from one major difference — the $. get() makes Ajax requests using the HTTP GET method, whereas the $. post() makes Ajax requests using the HTTP POST method.
AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser. It works on the browser or outside the browser also.
In this case, I'd say $.get
, as it's immediately clear what the type of request is. At any rate, it's just shorthand for the larger and more option-ified ajax call, and converting between the two is trivial in the worst case.
If you think that you'll need fancy $.ajax
options, use $.ajax
. If you don't use the convenience methods jQuery provides, such as .load
, $.get
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With