Using jQuery AJAX for GET Requests
Q: How do you perform a GET request using JQuery's AJAX method?
- JQuery
- Junior level question
Explore all the latest JQuery interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create JQuery interview for FREE!
You can perform a GET request using JQuery's AJAX method by using the `$.ajax()` function with the `type` option set to `'GET'`. Here's an example:
$.ajax({ type: 'GET', url: 'https://example.com/api/data', success: function(data) { console.log('Data received:', data); }, error: function() { console.log('Error receiving data'); } });
This code sends a GET request to the URL `'https://example.com/api/data'` using JQuery's AJAX method. The `type` option is set to `'GET'`, indicating that this is a GET request.
The `success` option specifies a function to execute if the request is successful. This function takes one argument, `data`, which contains the response data from the server. In this example, the function simply logs the response data to the console.
The `error` option specifies a function to execute if the request fails. In this example, the function simply logs an error message to the console.
You can also use the `$.get()` function to perform a GET request with a simpler syntax. Here's an example:
$.get('https://example.com/api/data', function(data) { console.log('Data received:', data); }) .fail(function() { console.log('Error receiving data'); });
This code also sends a GET request to the URL `'https://example.com/api/data'`. The `$.get()` function takes two arguments: the URL to request, and a callback function to execute when the request is complete. This function takes one argument, `data`, which contains the response data from the server.
The `.fail()` method is used to specify a function to execute if the request fails. In this example, the function simply logs an error message to the console.


