jQuery - Post

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.

jQuery $.post() Method

https://www.w3schools.com/jquery/ajax_post.asp
https://api.jquery.com/jquery.post/

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

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

$.post({url}}, {data}, {callback function name }});
  • The URL required parameter specifies the URL you wish to request
  • The optional data parameter specifies some data to send along with the request in the form of an anonymous object.
  • The optional callback parameter is the name of a function to be executed if the request succeeds.

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

Note: This should work but API is refusing the request. It seems to think it's not json. I wrestled with this for a couple of hours before deciding to move on. I even tried grabbing code-snippets from Postman and the issues just didn't stop. One after another. Using the ajax() method is the way to go.


$(document).ready(function ()
{
    let url = "https://city-info-api-prod.azurewebsites.net/api/v1.0/cities/ce09626b-e19e-47f5-82f3-2bbf60866a47/pointsofinterest";
    let data = { name: "New Jack City", description: "Where the priests are." };
    let successCallBack = (data, status) => { $("#demo1").html(data + "<br /><br />Status: " + status); };
    $("#btnPost").click(function ()
    {
        $.post(url, data, successCallBack, "json");
    });
});