Difference between ajax get and post
               Get |                Post |
GET request is used to retrieve data from the server. | POST request is used to send data along with the request on the server. |
GET requests may return cached data. | GET requests NEVER caches data. |
GET requests remain in the browser history.
| Post requests never remain in the browser history.
|
GET requests can be bookmarked.
| Post requests can never be bookmarked.
|
As discussed Get remain in the browser history so GET requests should never be used when dealing with sensitive data.
| Post requests should be used when dealing with sensitive data.
|
Get requests limit the length of the data that can be passed as part of the URL. | Post requests did not limit the length of the data that can be passed as part of the URL. |
Get method example:
xhttp.open("GET", "test.txt", true); xhttp.send(); |
Post method example:
xhttp.open("POST", "test.txt", true); xhttp.send(); |
Post method example with form data:
xhttp.open("POST", "test.jsp", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.send("fname=Roxy&lname=Malik"); |
Post method add an HTTP header with setRequestHeader() to post data like an HTML form. The send() contains the data which we want to send.