Getting Started with Ajax Using jQuery

jQuery provides Ajax support that abstracts away painful browser differences. It offers both a full-featured $.ajax() method and simple convenience methods such as $.get(), $.getScript(), $.getJSON(), $.post(), and $.fn.load().

Most jQuery applications don't in fact use XML; instead, they transport data as plain HTML or JSON (JavaScript Object Notation).

In general, Ajax does not work across domains. Exceptions are services that provide JSONP (JSON with Padding) support, which allow limited cross-domain functionality.

Data Types

jQuery generally requires some instruction as to the type of data you expect to get back from an Ajax request; in some cases the data type is specified by the method name, and in other cases it is provided as part of a configuration object. There are several options:

  • text – For transporting simple strings, usually to be placed directly into the page
  • html – For transporting blocks of HTML, usually to be placed directly into the page
  • xml – For transporting blocks of XML, which can be parsed to provide data
  • script – For adding a new script to the page; this type of request uses a concept called dynamic script tagging instead of XMLHttpRequest, and is therefore not limited by the same origin policy
  • json – For transporting JSON-formatted data, which can include JavaScript strings, arrays, and objects
  • jsonp – A variant of JSON, which can be used to retrieve content from other servers; this type of request also uses dynamic script tagging

It is recommended to use the JSON format in most cases, as it provides the most flexibility. It is especially useful when your server environment is PHP, since JSON is also native to that language.

Author: Chris Minnick
Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.