Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 2: Working with web services CHAPTER 8 373


Now that you know about the XMLHttpRequest object, be aware that browsers might
implement this object differently. Therefore, rather than write a large amount of code to deal
with differences, use one of the jQuery wrappers for XMLHttpRequest.

Using jQuery XMLHttpRequest wrappers
You can use jQuery to simplify AJAX calls and be browser independent. The jQuery meth-
ods can be executed synchronously or asynchronously. The following is a list of the available
jQuery methods; remember that the jQuery object is aliased to the dollar sign ($) to save typ-
ing so many characters. Where there is no dollar sign, the method can be executed on jQuery
objects that are referenced with a selector.
■■$.ajax() Low-level interface to perform an AJAX request
■■.ajaxComplete() Global AJAX event handler to register a handler to be called when
AJAX requests complete
■■.ajaxError() Global AJAX event handler to register a handler to be called when AJAX
requests complete with an error
■■$.ajaxPrefilter() Low-level interface that handles custom AJAX options before each
request is sent and before $.ajax() processes the requests
■■.ajaxSend() Global AJAX event handler that you use to attach a function to be
executed before an AJAX request is sent
■■$.ajaxSetup() Low-level interface that sets default values for future AJAX requests
■■.ajaxStart() Global AJAX event handler to register a handler to be called when the
first AJAX request begins
■■.ajaxStop() Global AJAX event handler to register a handler to be called when all
AJAX requests have completed
■■.ajaxSuccess() Global AJAX event handler to attach a function to be executed when-
ever an AJAX request completes successfully
■■$.get() Shorthand method to load data from the server by using an HTTP GET
request
■■$.getJSON() Shorthand method to load JSON-encoded data from the server by
using a GET HTTP request
■■$.getScript() horthand method to load a JavaScript file from the server by using a S
GET HTTP request and then execute it
■■.load() Shorthand method to load data from the server and place the returned
HTML in the matched element
■■$.param() Helper method to create a serialized representation of an array or object,
suitable for use in a URL query string or an AJAX request
■■$.post() Shorthand method to load data from the server by using an HTTP POST
request
Free download pdf