How to Perform a GET Request Using jQuery $.get()

This guide explains how to perform an asynchronous HTTP GET request using jQuery's built-in $.get() shorthand method. You will learn the core syntax, how to process returned data, pass query parameters, and handle network errors cleanly using modern promise-based chaining.

Basic Syntax

The $.get() method is a shorthand convenience method for the standard $.ajax() function. Its basic syntax is:

$.get(url, [data], [callback], [dataType]);
  • url: The destination endpoint to which the request is sent.
  • data (optional): A plain object or string sent to the server as query parameters.
  • callback (optional): A function executed if the request succeeds.
  • dataType (optional): The type of data expected from the server (e.g., xml, json, script, text, or html).

Basic GET Request Example

To retrieve data from an API or a local file, pass the URL and a success callback function:

$.get("https://api.example.com/users", function(data, status) {
    console.log("Status: " + status);
    console.log("Data received:", data);
});

When the request resolves successfully, the callback runs automatically. data contains the response body, and status contains the HTTP status text (such as "success").


Sending Data with the Request

You can pass query parameters by providing an object as the second argument. jQuery automatically serializes the object into a URL query string:

$.get("https://api.example.com/search", { query: "javascript", page: 1 }, function(response) {
    console.log("Search results:", response);
});

This request targets: https://api.example.com/search?query=javascript&page=1.


Handling Responses with Promises

The $.get() method returns a jqXHR object, which implements the Promise interface. Instead of passing inline callbacks, you can chain .done(), .fail(), and .always() methods for cleaner error handling and readability:

$.get("https://api.example.com/posts/1")
    .done(function(data) {
        // Runs on HTTP 200/success
        console.log("Post loaded:", data);
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
        // Runs on HTTP 4xx/5xx or network errors
        console.error("Request failed: " + textStatus, errorThrown);
    })
    .always(function() {
        // Runs regardless of success or failure
        console.log("Request completed.");
    });

Specifying the Expected Data Type

By default, jQuery intelligently guesses the response type based on the MIME type returned by the server. If you want to enforce JSON parsing explicitly, specify "json" as the final argument:

$.get("https://api.example.com/profile", function(user) {
    console.log("User name: " + user.name);
}, "json");