JQuery Load API: Get and Post Methods

When a page needs to display part of a server response, the request method determines how that response is handled. jQuery’s .load() method inserts a successful HTML response into selected page elements.

This article compares HTML loading with data requests and explains how to choose a method for JSON responses.

TL;DR

Use `.load()` when a successful response should become HTML inside selected elements, `$.get()` to retrieve data, and `$.post()` to send data to a server.

  • Choose `.load()` for an HTML fragment and its automatic insertion.
  • Choose `$.get()` for retrieval, including JSON when the response type is identified.
  • Choose `$.post()` to submit request data, then inspect the response or returned jqXHR.

What Is the jQuery Load API?

`.load()` is a jQuery collection method for placing a successful HTML response into matched elements.

Its signature is `$(selector).load(url, data, complete)`. Object data selects POST. Without it, the request uses GET.

Use `$.get()` or `$.post()` when the response needs application logic. Both return jqXHR objects for promise-style handlers.

How to Choose and Use jQuery Load, Get, and Post

Start with the server response you need and the HTTP action the endpoint expects. Keep the destination selector, payload and response type explicit enough that another reader can tell what the callback receives.

Step 1: Insert an HTML response with `.load()`

Call `.load()` on the element that should receive markup. This example requests a page fragment and checks the completion status before responding to an error.

$("#details").load("/products/42 #summary", function (response, status, xhr) {
  if (status === "error") {
    $("#message").text("Request failed: " + xhr.statusText);
  }
});

The space and selector after the URL ask jQuery to select `#summary` from the returned document. The callback receives response text, a status string and the jqXHR object. The demo uses this command:

node jquery_load_demo.js

Its output was:

status: success
inserted: Product: Desk lamp
Terminal showing the jQuery load demo success status and inserted product text

The status and inserted fragment match the expected completion behavior.

Step 2: Retrieve data with `$.get()`

Set the response type to JSON when the endpoint returns valid JSON, so the callback receives parsed data.

$.get("/api/products/42", function (product) {
  $("#name").text(product.name);
}, "json").fail(function (xhr, status) {
  $("#message").text("Could not load product: " + status);
});

Request JSON parsing with the final argument, then use `product.name` because rendering the whole object produces `[object Object]`.

Step 3: Submit data with `$.post()`

Use `$.post()` when the endpoint expects a submission. Provide the fields as an object and handle both a successful response and a rejected request.

$.post("/api/products", { name: "Desk lamp" }, function (created) {
  $("#message").text("Created product " + created.id);
}, "json").fail(function (xhr, status) {
  $("#message").text("Save failed: " + status);
});

The server must accept these fields and return valid JSON. Use `$.ajax()` when you need settings such as custom headers or a timeout.

Edge Cases and Next Steps

The `.load()` documentation says an empty matched collection prevents the request, so verify the destination selector when no request appears.

SymptomLikely causeNext check
No `.load()` requestSelector matched no elementsConfirm the destination exists before calling the method
`[object Object]` appearsWhole object was coerced to textRead a property or format the object as JSON
JSON callback failsResponse is invalid JSON or has the wrong typeInspect the response body and server content
Cross-domain request failsBrowser same-origin policy or server CORS configurationCheck protocol, host and port, then configure the server
Old `.load(handler)` code breaksIt referred to the removed load event shorthandUse `.on(“load”, handler)` for load events

Cross-origin access depends on browser same-origin rules and server CORS configuration. Switching from GET to POST does not grant permission.

The Ajax method is distinct from the removed event shorthand `$(window).load(handler)`. For a browser load event, use `.on(“load”, handler)`.

Conclusion

Match the method to the endpoint and response: use the API that fits the data flow, then provide a useful failure message.

See the jQuery references for `.load()`, `$.get()`, and `$.post()`. For related browser interactions, read how jQuery’s `.on()` method handles events and how to build a jQuery form.

FAQ

These answers address common questions about the load API and its response.

How do I load JSON with jQuery?

Use $.get() or $.ajax() for a JSON API response and specify the expected JSON type. The .load() method is intended to insert HTML into matched elements.

What is the difference between jQuery load and get?

.load() inserts a successful HTML response into selected elements. $.get() returns request data to a success handler and a jqXHR for promise-style handling.

Does jQuery load use GET or POST?

It uses GET when no object data is supplied, and POST when data is supplied as an object.

Sushant Dhiman
Sushant Dhiman

Sushant Dhiman writes beginner-friendly Flutter and Dart tutorials covering widgets, layouts, navigation, dialogs, and state. He also covers Node.js, Express, and jQuery fundamentals.

Articles: 14