84

What is the difference between GET and POST for Ajax requests?

I don't see any difference between those two, except that when I use GET, the parameters are send in URL, which for me don't really make any difference, since all requests are made on background and user doesn't find any difference.

edit: What are PUT and DELETE methods used for?

bool.dev
  • 17,508
  • 5
  • 69
  • 93
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
  • 9
    BTW, there are also PUT and DELETE requests in addition to POST requests. You should ask about those, also. – S.Lott Apr 03 '09 at 19:34
  • 1
    For future readers: here is a [related question by Fooker a year ago](http://stackoverflow.com/q/18395523/4043409). – Gideon Jul 08 '15 at 01:50
  • It DOES matter, especially when you're sending sensitive data. – biesior Jul 14 '20 at 12:42

10 Answers10

141

GET is designed for getting data from the server. POST (and lesser-known friends PUT and DELETE) are designed for modifying data on the server.

A GET request should never cause data to be removed from an application. If you have a link you can click on with a GET to remove data, then Google spidering your site could click on all your "Delete" links.

The canonical answer can be found here, which quotes the HTML 2.0 spec:

If the processing of a form is idempotent (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. Many database searches have no visible side-effects and make ideal applications of query forms.

If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.

In your AJAX call, you need to use whatever method your server supports. You should always design your server so that operations that modify data are called by POST/PUT/DELETE. Other comments have links to REST, which generally maps C/R/U/D to "POST or PUT"/GET/PUT/DELETE.

crb
  • 8,132
  • 6
  • 37
  • 48
  • 10
    +1: Essential definition of GET -- idempotency. All changes must happen with POST, PUT and DELETE. – S.Lott Apr 03 '09 at 19:36
  • my server is giving 403 error if I submit the form using post, get is working. I think this is due to server configuration. I don't have any access to the server. How to get around it ? – Krrish Raj Oct 25 '15 at 09:26
  • I agree with @S.Lott. Totally nice and completly definition of the GET method. /clap mate. – Victor Ribero Guasch Dec 16 '15 at 23:39
  • @KrrishRaj, it may have some thing to do with cross site posting. Take a look at this http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript/7605119#7605119 – Srikanth Jan 26 '16 at 16:51
  • 1
    Although I agree with the definition, in practice it is hard to implement because GET is so untrustworthy. GET is perfectly fine for files that infrequently change, but not so well for actual data ajaxing as a POST to change data, followed by a GET could return previously cached data, and not just by a browser, because ISP's can cache GET's and do not have to honor any caching headers. For files I use GET's, but with data querying and modifying, I always use POSTs. – Rahly Jun 02 '16 at 15:31
  • That being said, for a specific example where one has to make a request that will trigger an async process in the server (Let's say: create a report on the server), which method would be recommended? GET or POST? From one side, I'm tempted to say POST because it's creating a resource from the server perspective, but it seems weird to me. – rpbaltazar Oct 03 '16 at 08:42
28

If you're sending large amounts of data, or sensitive data over HTTPS, you will want to use POST. If it's just a simple parameter, I would use GET.

GET requests have a limit to the amount of data that can be sent. I forget the exact number, but this can cause issues if you're sending anything substantial.

Basically the difference between GET and POST is that in a GET request, the parameters are passed in the URL where as in a POST, the parameters are included in the message body.

Ryan Smith
  • 8,344
  • 22
  • 76
  • 103
  • 2
    yep, important to point out that there are size limitations associated with GET and that they are different depending on the client and server software – Allen Rice Apr 03 '09 at 19:45
19

Whether its AJAX or not is irrelevant. Its about the action that you're taking. I'd recommend following the principles of REST. Which have further provisions for updating, deleting, etc...

Brian
  • 4,931
  • 3
  • 32
  • 55
4

Both are used to send some data and receive some response using that data.

GET: Get information store in server. Ie. Search, tweet, Person Information. If you want to send information then get request send request using process.php?name=subroto So it basically send information through url. Url cannot handle more than 2083 char. So for blog post can you remember it is not possible?

POST: Post do same thing as get. User registration, User login, Big data send, Blog Post. If you need to send secure information then use post or for big data as it not go through url.

AJAX: $.get() and $.post() contain features that are subsets of $.ajax(). It has much configuration.

$.get () method, which is a kind of shorthand for $.Ajax (). When using $.get (), instead of passing in an object, you pass in arguments. At minimum, you’ll need the first two arguments, which are the URL of the file you want to retrieve (i.e. ‘test.txt’) and a success callback.

Summary:

$.get( url [, data ] [, success ] [, dataType ] )
$.post( url [, data ] [, success ] [, dataType ] ) // for sending secure or Large information
$.ajax( url [, settings ] )  // More Configaration
Subroto Biswas
  • 553
  • 7
  • 5
4

GET requests are easier to exploit in CSRF (cross site request forgery) attacks. Namely fake POST requests require Javascript to be enabled on the user side, while fake GET requests are still possible just with img, script tags.

Sergey
  • 2,906
  • 3
  • 27
  • 32
3

Many web servers limit the length of the data that can be passed as part of the URL, so the GET request may break in odd ways that are hard to debug.

Also, most server software logs URLs in the access logs, so if you pass sensitive information (such as passwords) in a GET request, this will in all likelihood be written to disk in plaintext.

From a REST perspective, GET requests should have no side-effects -- they shouldn't modify data. So, if you're just GETting a resource by ID, this makes sense, but if you're committing changes to a resource, you should be using PUT, POST, or UPDATE for the http verb.

Don Werve
  • 5,100
  • 2
  • 26
  • 32
2

First, general information. Use GET if you only read data, use POST if you change something on database, txt files etc.

But the problem is, some browsers cache GET results. I had problems with AJAX requests in IE7, but at last I found out that browser caches GET results. I rethought the flow and changes my request to POST.

So, don't use GET if you don't want caching.

(Of course you can disable caching in GET operations. But I didn't prefer it)

trante
  • 33,518
  • 47
  • 192
  • 272
1

About me, i prefer POST. I reserve get to the events i know the sent value is limited to data i have the "control", for example, to retreive an item with an id. Example, "getitem?id=123", "deleteImtem?id=123", ... For the other cases, when i have a form fillable by a user, i prefer POST.

Like Ryan Smith have said, it's better to use POST to send a large amount of data, and less wories in cases of the use in others language/special chars (generally all majors javascript framework should'nt have any problems to deal with that but i think is less wories to use POST).

For the REST perspective, in my opinion, you can use this with a new project (to keep a consistency with the entire project).

Finally, maybee some programs used in a network (URL loguers (ie.: to see if the employees lost their time on non-autorised sites, ...) proxys, ... ) or any other kind of tool can intercept the query. Somes will show in the reports the params you have sent with GET, considering it like a different web page. But in this situation, is could be not your problem it's changes from a project to an other! ;)

user86830
  • 63
  • 3
0

The difference is the same between GET and POST whether you're using Ajax, HTML forms, or curl. Here are the relevant definitions:

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
-2

If you are passing on any arguments with characters that can get messed up in the URL (such as spaces), you use POST. Otherwise you can use GET.

Generally, if you're just passing on a few tiny arguments you would use GET. But for passing on user submitted information such as blog entries, text, etc, its a good practice to use POST.

There are also certain frameworks that rely completely on segment based urls (such as site.com/products/133 rather than site.com/products.php?id=333 and these frameworks unset the GET variables for security. In such cases you would use POST allt the time.

Ali
  • 261,656
  • 265
  • 575
  • 769