In this article, we’ll discuss how to make a jQuery ajax GET example with parameters. In Jquery we’ve got several functions to issue Ajax requests. We’ve got a load, get, and post.
All these functions i.e load, get, and post actually calls the Jquery Ajax function. These wrapper methods are easier to use, but they do not provide much flexibility.
Ajax method provides us full control over the Ajax request and we can also configure the behavior of the Ajax request. Below is the syntax of the Jquery Ajax method.In the Ajax method, pass a single JavaScript object, with all the options that define the behavior of the Ajax request.
We have created an HTML and on this page, we have all the options available that we can use with this Jquery Ajax ,For our demo purpose, we have a rest API that returns the list of tourists in a travel agency. Below is the response of API.
Now I want to fetch data from API and display it in HTML Table for that I’m going to use the ajax function.
Ajax call rest API with parameters
Ajax GET example with parameters, Let’s take an example where we need to pass the parameter in the rest API.
Api Url: http://restapi.adequateshop.com/api/Tourist/7
Json Response:
{
"id": 86,
"tourist_name": "ap",
"tourist_email": "ap.prak96@gmail.com",
"tourist_profilepicture": "http://restapi.adequateshop.com/Media//Images/userimageicon.png",
"tourist_location": "USA",
"createdat": "2020-06-02T05:18:22.7652253"
}
How to pass parameters in GET requests with jQuery
Specify the URL to which you want to make a request, then you use this URL option. specify whether you want to issue a GET or a POST request.
you want to issue a get request, you specify GET. If it is POST, then specify POST. For calling a function when the request completes successfully, we have success option in the Ajax method. If any error occurred in the request, then we have the error option. using data option we can parameters in the ajax method.
$.ajax({
url: 'http://restapi.adequateshop.com/api/Tourist',
method: 'GET',
dataType: 'json',
data: {
id: 86,
},
contentType: 'application/json; charset=utf-8',
success: function (result) {
debugger;
if (result != null) {
var tablerow = "<tr>"
+ "<td>" + result.id + "</td>"
+ "<td>" + "<img style='width:20px;border-radius:50px' src=" + result.tourist_profilepicture + ">" + result.tourist_name + "</td>"
+ "<td>" + result.tourist_email + "</td>"
+ "<td>" + result.tourist_location + "</td>"
+ "</tr>";
$("#tblbody").append(tablerow);
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
})
Real-time Example
API Calling:
<!DOCTYPE html>
<html>
<head>
<title> Call Get api using ajax </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1> Calling rest api from javascript/jquery </h1>
<br />
<fieldset>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>TouristName</th>
<th>TouristEmail</th>
<th>Location</th>
</tr>
</thead>
<tbody id="tblbody"></tbody>
</table>
</fieldset>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
GetTouristById()
});
//get tourist information by id
function GetTouristById() {
$.ajax({
url: 'http://restapi.adequateshop.com/api/Tourist',
method: 'GET',
dataType: 'json',
data: {
id: 86,
},
contentType: 'application/json; charset=utf-8',
success: function (result) {
debugger;
if (result != null) {
var tablerow = "<tr>"
+ "<td>" + result.id + "</td>"
+ "<td>" + "<img style='width:20px;border-radius:50px' src=" + result.tourist_profilepicture + ">" + result.tourist_name + "</td>"
+ "<td>" + result.tourist_email + "</td>"
+ "<td>" + result.tourist_location + "</td>"
+ "</tr>";
$("#tblbody").append(tablerow);
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
})
}
</script>
If you have knowledge of JavaScript, HTML, JSON, and XML then you can easily learn AJAX very easily.
AJAX uses XHTML for content and CSS for presentation, along with Document Object Model and JavaScript for dynamic content display.
AJAX is a technology with the help of which the information is brought from the server to the page without refreshing the page ,is used a lot in creating a dynamic website because all the processing of AJAX happens in the backend and information can be brought from the server to the page without reloading the page.
This is a function of jQuery’s AJAX, with the help of which you can send data from the page to the server.
URL – In this, you have to give the path of your rest API or URL in which your server Code is written.
Type – In this you have to send a request from both Get and POST.
Data – In this you have to send your data, here you can also send Array and Variable.
Datatype – In this, you can give JSON because only JSON is used to send Array in AJAX, if you do not send Array then it is not necessary to write datatype.
Cache – You have to keep this False otherwise the browser can send the request from its own cache and an error can come.
Success – If your Function is successful, then whatever data you get back from the PHP file will come in a variable called S of Success Function.
Error – If an error occurs in Ajax, then that error message will come in the err variable.
The post jQuery Ajax GET Example with Parameters appeared first on Software Development | Programming Tutorials.
Read More Articles
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- jQuery Ajax GET Example with Parameters
- How to Pass Parameters in AJAX POST?| JQuery Ajax Post Json Example
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- How to add, edit and delete rows of an HTML table with Jquery?
- Registration form with image upload in MVC using jquery Ajax
- How to make an Inline editable table in MVC using jquery?
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- [Solved]-How to Upload pdf file using jquery MVC?
- Dynamically creating graphs with jQuery
- Canvas.toDataURL() not working in IE having SVG content in Canvas
- get jquery data from a list element that has a class starting with "active"
- jQuery .on() method woking on Firefox but not on Chrome
- Textbox Losing value under Jquery Modal Dialog Box
- How to set php mysql from javascript var or input type text?
- load multiple images on click using javascript or jquery?
- How to get id of html element in top level html tag
- How to implement selenium framework using jQuery ? Any ideas?
- jQuery automatically animate div resize
- Add multiple values to cookie with jQuery
- Jquery form submit empty values to Mysql
- Redirect user to page while sending custom IP adress with post
- How to pass value as array by input field
- Text variable as html from file from another site
- Highlight the specific area of the image using .maphilight()
- Capture click event before page load and trigger after DOM ready
- Javascript don't count variables in tax app
- How to display modal popup when the if condition is true in View without using Onclick etc? using mvc .net
- jQuery scroll div when mouse not over it
- jquery datatable highlight drops off after reload
- Multiple clickeble pdfs in a fancybox
- Trouble with FullCalendar rendering events using .getJSON() from controller action
- Multi-select dropdown menu
- optimize jquery while referencing specific classes
- Saving input values in HTML markup
- Issue with Mobile Menu on Website
- Change the value of many Divs on select of drop-down
- dynamic form not submitting file
- Autocomplete: How to restrict the number of fields shown in the option box?
- How to return new markers coordinates changed in separated window?
- How to redirect with document ready function in jquery
- Delete localStorage after 1 hour and re-fetch data
- Retrieve links from divs and displaying them in another container right below the first div
- jQuery scrollSpeed Plugin not working for inner multiple scrollable div
- Faster coding for Mouse up, Mouse down function
- To display dates continuously in a table column automatically
- Multiple Sticky Header and Sidebars
- How to post one more parameters to action MVC jquery
- JQuery encodeURIComponent page not found error
- show json file in an alert on click
- jQuery syntax error on line 1715
- Styling Dropdown: Get and sum height of sibling elements
- jquery input field focusout div
- Jquery Ajax request is wrong - something is missing or incorrect
- jQuery Validate plugin doesn't work in CodeIgniter
- Jquery Popup plugin closes when clicked anywhere
- Building ajax post with jQuery each()
- Blue Menu does not appear during Pop Up in Chrome
- Method of object as handler
- Why is this Bit.Ly script not working?
- Why cross domain ajax post doesn't work?
- Run a php line again after updating a row in the database
- Jquery tab inside tab activate by subtab index
- continue the .each() loop in jQuery to show more search results
- jquery and Jplist jquery plugin table data with many pages
- Reload bx-slider in commonJS module
- CSS3 Translating different sized content from same starting position
- fullcalendar is shifting dates from the database
- kendo ui grid get nulled on first data when hit save on toolbar
- Scroll content of second div to end of first div
- jquery sortable from custom event
- Add query string to url using jquery
- Trouble passing Rails Data into a JavaScript Function on mouse over..? Failed attempt:
- Jquery animate is not working within for loop
- Using JQuery to wrap and append alternative html content depending on the type of class that the child image element has
- Access the Values in this Array [API Result]
- zoomIn effect (Animate.css) doesnt works in second click or without page load
- JQ grid Date format(Month date, year) Ex- June 26, 2015
- find next tbody in the same table and hide only the next ones
- Auto refresh JS with appendchild command that grabs data from an external file
- ajax: Is it possible to "POST" and "GET" at the same time?
- jQuery .slice() limitation- prev/next button hide
- how to get the html file of an edited canvas in html5
- Unable to get the response from an ajax call in the success function
- Cost of Initializing jQuery Sortable Multiple Times
- How to open text links in magnific-popup?
- How to prevent new line from showing on screen
- Problems with option text not showing in Medium editor (from Github)
- jQuery fadeIn() not giving the result I want
- Filtering div's on page using drop down list jquery plugin
- sending multiple variables in url for php with jquery ajax post call
- Use content_for to insert
- How to find a video element within an owl-carousel slide and then set owl-carousel control's css only for that specific slide
- Sort date in table
- adding object key and value to URL Query
- disabled touch on mobile owl carousel
- Event when space is pressing, only one time
- Jquery TimeCircles Value when stopped
- How work jQuery DataTable serverSide option
- JQuery CSS Transform Working in Chrome and Firefox but Not IE
- Passing data from attribute values into tooltip
- jQuery tooltip random appear
- Dynamically add value in textbox on focus of the last created dynamic textbox using jquery
- Creating a website setup 'wizard'
- Unable to set top/left properties on dynamically generated div through jquery
- I would like Slit Slider to stop on last slide
- Mouse pointer stops the slide transition in fwslider
- JQuery - Populating selection list from database using JDBC
- Unexpected token j in JSON (Full Calendar)
- How to apply a range date filter on angular js year or month?
- Wordpress Modification To A Five-Star Rating Script
- jquery, columns equal heights and at least as tall as the browser window
- How to preserve the values from client-side after postback
- Hide div and apostrophes occur
- Blinking of Bootstrap Tabs in some of pages
- bootbox.dialog - The main button is not getting focus when using ScriptManager.RegisterStartupScript
- Get product title by given permalink , Woocomerce
- seems that login-page is reloading after false form-validation
- Laravel send data to mysql using ajax
- How to use data from Facebook FQL results
- setting top and left with offset()
- Immidiately remove a button from HTML page when a user chooses to delete it
- Change dropzone thumbnail with cropped canvas image canvas null error
- Convert JSON array into jQuery array format
- Utilising jQuery Response From User Entered Form Data
- Modifying a simple jQuery multiplier
- Web form that displays user input on same page without page reload
- AJAX JQuery Database
- Trying to Validate that an input is not empty then link to another page if it is not, can't get link to work correctly
- JQuery can't access elements created using DOM
- Make a slider with a div
- how do I repeat table with client side template
- CDN does not work from master page and related pages with content
- Drag n Drop using jQuery UI
- Toggle Divs but show only one at once
- Adding MySQL requests to JS Code from JSFiddle
- jQuery simple loading and unloading content into DIV
- AJAX History Trouble
- Bootstrap Justified Nav Bar
- Is it possible to display alternative text for selected items in a Select2 MultiSelect?
- How to get values from ArrayList<String>, using text fields for entering the startValue and EndValue?
- jqgrid will not load json data from remote URL
- When I use an href to return to a previous page, part of the page flashes before the page is painted
- Open colorbox on a non-link / href?
- setGridHeight auto on jqgrid
- Why am I getting array how to get string
- pulling selected radio value may be giving wrong button selected?
- Highcharts showing points on hovering over the line which are outside the chart area when the chart is zoomed in
- Questionable Positioning of drawn Lines in Canvas
- jQuery slice random amount of divs
- scrolling to a location on div Id using jquery?
- Select only first element using :contains or similar
- trying to use sidr for two sidebars
- Save the result of a JSONP callback to an outer object
- How to simplify jQuery code , plugin, scroll to the block
- change event not firing in IE for the first time
- Unable to loop through JSON Data
- How to combine slider range and some values
- Getting Carmen-Rails Subregion select to work correctly
- Service method name is not valid. from Hosted server