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