Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save natevenn/3a03ee0159b9462d95014b385a9cd2c1 to your computer and use it in GitHub Desktop.
Save natevenn/3a03ee0159b9462d95014b385a9cd2c1 to your computer and use it in GitHub Desktop.
Ajax is a way to load data on the browser without reloading the page
It uses a browser's built-in XMLHttpRequest
jQuery has $.ajax() method
A is for asynchronous
jax is for JS and XML
Today most servers return JSON which is a subset representation of a JS object in string format
jQuery handles the parsing of JSON into JS for us.
jquery also has convenient methods if you dont care about error handling.
.get() and .post()
these methods take a url, an optional data object, and an optional callback function to handle successes
We can send data with our request by setting the data property on our configuration object,
or by passing an object as the second argument to one of the convenience methods.
for the get request the data is appended to the end of the url as a query string
for a post use the .serialize() on the object when configuring the data
the returned object from an ajax call can be stored into a variable
with the convenience methods use the .then() as a callback for how to handle successes and errors.
it takes two functions. first is how to handle success and second for errors
or to call success and error functions seperately use .done() for success and then .fail() for errors
sometimes XHRs to other domains are blocked by browsers. to get around this some third party apis use JSONP which allow access to
data on another server. To use JSONP specfy in the object configuration - dataType: 'jsonp'
dont understand how deferred works and why you would use it. Looks like its just another way to manage asynchronous code and
reduce deeply nested functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment