jQuery - Get

The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request. Two commonly used methods 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.

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

$.get({url}}, {callback function name }});

The required URL parameter specifies the URL you wish to request. The optional callback parameter is the name of a function to be executed if the request succeeds.

http://www.w3schools.com/jquery/jquery_ref_ajax.asp

The first attempt will be slow. The API will have to 'wake up'.


$(document).ready(function ()
{
    let output = "";
    let url = "https://city-info-api-prod.azurewebsites.net/api/v1.0/cities?pageNumber=1&pageSize=10";

    $("#btnGet").click(function ()
    {
        // getting api data
        $.get(url, function (apiData, status)
        {
            output += `Status: ${status}<br />`;

            apiData.forEach(AddCityToOutput);

            $("#demo-content").html(output);
        });
    });

    const AddCityToOutput = (city) =>
    {
        output += `${city.name}, ${city.cityId} <br />`;
    };
});