WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

jQuery - AJAX get() and post() Methods


The jQuery get() and post() strategies are utilized to ask for information from the server with a HTTP GET or on the other hand POST request.


HTTP Request: GET versus POST

Two generally utilized strategies for a demand reaction between a customer and server are: GET and POST.

  • GET - Requests information from a predefined resource
  • POST - Submits information to be handled to a predefined resource

GET is essentially utilized for simply getting (recovering) a few information from the server. Note: The GET strategy may return reserved data.

POST can likewise be utilized to get a few information from the server. Be that as it may, the POST strategy NEVER stores information, and is frequently used to send information alongside the request.

To get familiar with GET and POST, and the contrasts between the two strategies, if you don't mind read our HTTP Methods GET versus POST chapter.


jQuery $.get() Method

The $.get() strategy demands information from the server with a HTTP GET request.

Syntax:

$.get(URL,callback);

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

The discretionary callback parameter is the name of a capacity to be executed on the off chance that the demand succeeds.

The following precedent uses the $.get() technique to recover information from a document on the server:

Example

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

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

The second parameter is a callback work. The primary callback parameter holds the substance of the page asked for, and the second callback parameter holds the status of the request.

Tip: Here is the manner by which the ASP document resembles ("demo_test.asp"):

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

jQuery $.post() Method

The $.post() technique demands information from the server utilizing a HTTP POST request.

Syntax:

$.post(URL,data,callback);

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

The discretionary information parameter determines a few information to send alongside the request.

The discretionary callback parameter is the name of a capacity to be executed in the event that the demand succeeds.

The following precedent uses the $.post() technique to send a few information alongside the request:

Example

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

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

Then we go in certain information to send alongside the demand (name and city).

The ASP content in "demo_test_post.asp" peruses the parameters, forms them, and returns a result.

The third parameter is a callback work. The primary callback parameter holds the substance of the page asked for, and the second callback parameter holds the status of the request.

Tip: Here is the means by which the ASP document resembles ("demo_test_post.asp"):

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