jQuery Basic

jQuery Home jQuery Introduction jQuery Getting Started jQuery Syntax jQuery Selectors jQuery Events

jQuery Effects

jQuery Show/Hide jQuery Fade jQuery Slide jQuery Animation jQuery Stop jQuery Chaining jQuery Callback

jQuery manipulation

jQuery Get/Set jQuery Insert jQuery Remove jQuery CSS Classes jQuery Style Properties jQuery Dimensions

jQuery advanced

jQuery Traversing jQuery Ancestors jQuery Descendants jQuery Siblings jQuery Filtering jQuery Ajax jQuery Load jQuery Get/Post jQuery No-Conflict

jQuery Exercises

jQuery Practice Examples jQuery Quiz

jQuery References

jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties

jQuery Ajax GET and POST Requests

We will learn how to send and receive data from the server through Ajax via HTTP GET or POST methods using jQuery.

jQuery $.get() and $.post() Methods

there are Two methods commonly used for a request-response between a client and server are: GET and POST.

  • GET -Requests data from a specified resource
  • POST -Submits data to be processed to a specified resource

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.

POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

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.

$.get(URL, data, success);
β€”Orβ€”
$.post(URL, data, success);

The parameters in the above syntax have the following meaning:

  • The required URL parameter specifies the URL to which the request is sent.
  • The optional data parameter specifies a set of query string (i.e. key/value pairs) that is sent to the web server along with the request.
  • The optional success parameter is basically a callback function that is executed if the request succeeds. It is typically used to retrieve the returned data.

Let's see some overview on HTTP Methods

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server.

Note: A client (browser) sends an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.

HTTP Methods

  • GET
  • POST
  • PUT
  • HEAD
  • DELETE
  • PATCH
  • OPTIONS
  • CONNECT
  • TRACE

The two most common HTTP methods are: GET and POST.

The GET Method

GET is used to request data from a specified resource. the query string (name/value pairs) is sent in the URL of a GET request

Some notes on GET requests:

  • GET requests can be cached
  • GET requests remain in the browser history
  • GET requests can be bookmarked
  • GET requests should never be used when dealing with sensitive data
  • GET requests have length restrictions
  • GET requests are only used to request data (not modify)

The POST Method

POST is used to send data to a server to create/update a resource. The data sent to the server with POST is stored in the request body of the HTTP request.

Some notes on POST requests

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length

Compare GET vs. POST

The following table compares the two HTTP methods: GET and POST.

  GET POST
BACK button/Reload Harmless Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
Encoding type application/x-www-form-urlencoded application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
History Parameters remain in browser history Parameters are not saved in browser history
Restrictions on data length Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) No restrictions
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also allowed
Security GET is less secure compared to POST because data sent is part of the URL

Never use GET when sending passwords or other sensitive information!
POST is a little safer than GET because the parameters are not stored in browser history or in web server logs
Visibility Data is visible to everyone in the URL Data is not displayed in the URL

jQuery $.get() Method

The $.get() method requests data from the server with an HTTP GET request.

$.get(URL,callback);

the jQuery $.get() method to make an Ajax request to the "demo_test.asp" file using HTTP GET method. It simply retrieves and returned from the server and displays it in the browser without refreshing the page.

Example

$("button").click(function(){
  $.get("demo_test.asp", function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});
Try it Yourself

The first parameter of $.get() is the URL we wish to request ("demo_test.asp").

The second parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.

Here is how the ASP file looks like ("demo_test.asp"):

<%
response.write("This is some text from an external ASP file.")
%>

jQuery $.post() Method

The $.post() method requests data from the server using an HTTP POST request.

Syntax:

$.post(URL,data,callback);

The required URL parameter specifies the URL you wish to request.

The optional data parameter specifies some data to send along with the request.

The optional callback parameter is the name of a function to be executed if the request succeeds.

The example to uses the $.post() method to send some data along with the request:

Example

$("button").click(function(){
  $.post("demo_test_post.asp",
  {
    name: "Donald Duck",
    city: "Duckburg"
  },
  function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});
Try it Yourself

The first parameter of $.post() is the URL we wish to request ("demo_test_post.asp").

Then we pass in some data to send along with the request (name and city).

The ASP script in "demo_test_post.asp" reads the parameters, processes them, and returns a result.

The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.

Here is how the ASP file looks like ("demo_test_post.asp"):

<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & " . ")
Response.Write("Hope you live well in " & city & " . ")
%>

We will learn about jquery no conflict method in next chapter. Click