Contents
Request how to make API Calls
Hi everyone, today we will post an example of how to send a request to the server via jQuery using methods POST and GET. In this example we have to use jQuery also two types of data, one is JSON data, and another is a PHP file, with some data sorted into array, and then converted into JSON format.
So first all we need to do is to to C:\xampp\htdocs\ and create the back-end directory and then from desktop directory we will make the request. As I’m checking until now the Old school tutorials use the CDNS of jQuery or others services PLEASE TRY TO BE MORE MODERN ON DEVELOPING, so it is very easy in that way. Make sure you have installed Node JS to your computer or if you want follow the link to download it Node JS.
So lets create a directory, C:\xampp\htdocs\request and inside this folder we will create two separated directories frontend and backend. From frontend we will make requests API to the backend. And lets sort all the data into a HTML table.
The HTML Structure
<!DOCTYPE html> <html> <head> <title>POST and GET Request</title> </head> <body> <table id=""></table> <script src="node_modules/jquery/dist/jquery.min.js"></script> <script> //post request </script> </body> </html>
The formats of both requests should look like below:
$.post("<ulr>",{data:data}, function(response){ console.log(response); });
Get method in api
$.get("<ulr>",{data:data}, function(response){ console.log(response); });
In case that we have to request JSON file, its better to call via GET method, because generally JSONs files are mostly PUBLIC data, such as countries data, ips data etc. In case that we want to use the call more securely, POST its better. On both types we can send data and callback data as response.
Difference between Get and Post
In Get method all parameters and data are sent or requesting to the URL/URI of request. In Post method all parameters and data are sent or requesting to the Body of request.
Get method is more easy accessible, that’s why its used when a service used to provide public data, such as Country List, Name List, planets list, road list etc. Post method its use more security parameters and its used more to provide to the users generally 3rd party services, premium services that need API keys, Payments methods etc.
JSON file should have this format of data as below:
{ "person": { "name": "Michael", "surname": "Idol", "age":"25", "country": "France", "city": "Paris" }, "developerskills":{ "html":"Expert", "css":"Expert", "javascript":"Intermediate", "php":"Expert" }, "cars":{ "car1":"Toyota", "car2":"Mercedez", "car3":"Ferrari" } }