Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax() vs get()/post()

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) { }); 
like image 591
Richard Knop Avatar asked Aug 27 '09 23:08

Richard Knop


People also ask

What is the difference between jQuery get () and jQuery ajax ()?

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).

Is Ajax request GET or POST?

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.

What is difference between Ajax and jQuery?

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.


1 Answers

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.

like image 135
Stefan Kendall Avatar answered Sep 28 '22 00:09

Stefan Kendall