Reputation: 1229
I'm trying to understand AJAX and JSON and I'm not sure I get it, there are methods and functions that are doing the same stuff...
You've got $.getJSON
to retrieve JSON format data from server, and you have $.ajax
+ $.post
+ $.get
+ load()
to send data data to the server?
Can I use all of those methods to send JSON data?
Really I'm confused! Help me figure this out.
Upvotes: 1
Views: 213
Reputation: 97718
An AJAX request is, at heart, an HTTP request. This is the same protocol which is used for all content on the Web (arguably, if it's not HTTP, it's not the Web) - loading a page, the images on the page, the CSS and JS includes, a submitted form, etc, etc.
As such, it inherits pretty much all of the flexibility of HTTP, meaning a generic function like jQuery.ajax ends up quite complex, with lots of options you don't normally need to worry about. This leads to the Shorthand Methods you mentioned, which bundle up common sets of options and functionality.
Among the things you might want to vary:
All of the above are possible with jQuery.ajax, but you'd have to remember the parameters, even though you're falling into the same cases again and again, so chances are most of the time you'll be using whichever of the shorthands suits your needs at that moment.
Upvotes: 2
Reputation: 664528
All those are just shorthands for calling the $.ajax
function.
load
is for retrieving HTML and writing it to the DOM in one go. You want to load JSON.get
and getJSON
use GET
requests which are not well-suited for sending JSON data.post
does a POST
request, but doesn't allow you to choose the contentType
of the sent dataFor sending JSON you should use the $.ajax
function with its many options, see Send JSON data with jQuery.
Upvotes: 2