2

When do you use JQuery's Ajax method load and get? They seem to achieve the same things? Anybody knows? Thank you very much.

I'm not asking the difference between get and post methods. I'm asking Jquery get vs Jquery load methods.

djangoscholar
  • 133
  • 2
  • 11
  • @ErikvonAsmuth Guess OP is asking about about load vs get. – Suresh Atta Jan 01 '18 at 16:19
  • 1
    The docs have your answer: http://api.jquery.com/load and http://api.jquery.com/jquery.get. TL;DR: they both make a GET AJAX request, however `load` retrieves a portion of the requested page matching the selector you provided and appends it in to the selected element – Rory McCrossan Jan 01 '18 at 16:20
  • Have you checked the documentation? – Pointy Jan 01 '18 at 16:21
  • get vs load is like each vs map. You could call get and then put the data to an element, or just let load do it. Just like you could create an array, and then use each to push elements to it, or just let map do it. It's the difference between low level methods that can be used for anything, vs methods that have a more specific purpose. – Taplar Jan 01 '18 at 16:22

1 Answers1

0

To start with load uses get internally. When your server returns plain html and you want to set that html completely to an element, you can use load. Otherwise get or post

http://api.jquery.com/load/

This method is the simplest way to fetch data from the server. It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • so that is probably why they use Jquery get() when data returned like a queryset is returned instead when plain html is returned load is used. – djangoscholar Jan 01 '18 at 16:26
  • 1
    You don't *have* to use load. You can use get. It's just load is made specifically for that use case. – Taplar Jan 01 '18 at 16:28