Why are there GET and POST requests in AJAX as it does not affect page URL anyway? What difference does it make by passing sensitive data over GET in AJAX as the data is not getting reflected to page URL?
Well, as for GET, you still have the url length limitation. Other than that, it is quite conceivable that the server treats POST and GET requests differently; thus the need to be able to specify what request you're doing.
Two primary reasons for having them:
GET
requests have some pretty restrictive limitations on size;POST
are typically capable of containing much more information.The backend may be expecting
GET
orPOST
, depending on how it's designed. We need the flexibility of doing aGET
if the backend expects one, or aPOST
if that's what it's expecting.
You should use the proper HTTP verb according to what you require from your web service.
When dealing with a Collection URI like: http://example.com/resources/
GET: List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.
PUT: Meaning defined as "replace the entire collection with another collection".
POST: Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.
DELETE: Meaning defined as "delete the entire collection".
When dealing with a Member URI like: http://example.com/resources/7HOU57Y
GET: Retrieve a representation of the addressed member of the collection expressed in an appropriate MIME type.
PUT: Update the addressed member of the collection or create it with the specified ID.
POST: Treats the addressed member as a collection in its own right and creates a new subordinate of it.
DELETE: Delete the addressed member of the collection.
Source: Wikipedia
You normally send parameters to the AJAX script, it returns data based on these parameters. It works just like a form that has method="get" or method="post". When using the GET method, the parameters are passed in the query string. When using POST method, the parameters are sent in the post body.
Generally, if your parameters have very few characters and do not contain sensitive information then you send them via GET method. Sensitive data (e.g. password) or long text (e.g. an 8000 character long bio of a person) are better sent via POST method.
Others have covered the main points (context/idempotency, and size), but i'll add another: encryption. If you are using SSL and want to encrypt your input args, you need to use POST.
Another difference between GET
and POST
is the way caching is handled in browsers. POST
response is never cached. GET
may or may not be cached based on the caching rules specified in your response headers.
When we use GET method in AJAX,only the content of the value of the field is sent ,not the format in which the content is.For example ,content in text area is just added in url in case of GET method(without new line character).That is not the case in POST method.
Thanks.. I mainly use GET method with ajax and I haven't got any problems until now except the following: IE (unlike FF and Google Chrome) cache GET calling if using same GET values. So, using some interval with ajax GET can show same results unless you change URL with irrelevant random number usage for each ajax GET.