GET vs empty POST
For my job, I was looking through a few javascript files, and found a few AJAX calls that used POST, but didn't send any data. It seems they used to, but the backend was updated, and the data wasn't needed, and the previous developers left them as POSTs (or they just copied & pasted the $.ajax
calls from other files, and removed the data values).
I changed these empty POST requests to GET requests. I'm assuming it's more efficient to use a GET instead of an empty POST. I've looked, and haven't found anything useful.
So, is it more efficient to use a GET instead of a POST with no data se开发者_JAVA百科nt?
I would argue that GET vs. POST isn't an efficiency question so much as a semantic question. What is the intent of the POST requests? If the intent is to alter the state of the system in some way, then I would recommend leaving them as POSTs. If the intent is simply to retrieve some data from the system, then I would change them over to GETs.
The issue of data parameters doesn't really come into play as both GET and POST requests can accept parameters. (GET on the query string and POST via post data)
Outside of theoretical concerns, there are real reasons to use GET or POST. For instance, GET requests can be cached by web servers, proxy servers and clients, whereas POST requests are never cached AFAIK. I'm sure there are other differences, but adhering to the semantic nature of the requests should take care of them for you.
Rather than performance reasons it is the difference of the meaning of the two verbs. GET is supposed not to change the requested resource, whereas POST might do.
Other than the fact that GET
has one character less than POST
I doubt there is any performance difference. Both requests have the exact headers (except the method part) and no body. They are almost identical.
For example:
GET /someResource.ext HTTP/1.1
Accept: text/plain
Accept-Encoding: gzip
// empty line //
versus
POST /someResource.ext HTTP/1.1
Accept: text/plain
Accept-Encoding: gzip
// empty line //
But when choosing one over the other you should keep in mind their purpose. GET should be used when you need to retrieve something from the server while POST should be used when you need to send something to the server (send as in give, not as in sending a parameter).
精彩评论