02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Return Value<br />

The XMLHttpRequest object that was created.<br />

Description<br />

This is a shorthand AJAX function, which is equivalent to:<br />

$.ajax({<br />

url: url,<br />

dataType: 'json',<br />

data: data,<br />

success: success<br />

});<br />

[ 159 ]<br />

Chapter 7<br />

The callback is passed the returned data, which will be a JavaScript object or array as<br />

defined by the JSON structure and parsed using the eval() function.<br />

For details on the JSON format, see http://json.org/.<br />

Most implementations will specify a success handler:<br />

$.getJSON('ajax/test.json', function(data) {<br />

$('.result').html('' + data.foo + '' + data.baz[1]<br />

+ '');<br />

$().log('Load was performed.');<br />

});<br />

This example, of course, relies on the structure of the JSON file:<br />

{<br />

}<br />

“foo": “The quick brown fox jumps over the lazy dog.",<br />

“bar": “How razorback-jumping frogs can level six piqued gymnasts!",<br />

“baz": [52, 97]<br />

Using this structure, the example inserts the first string and second number from the<br />

file onto the page. If there is a syntax error in the JSON file, the request will usually<br />

fail silently; avoid frequent hand-editing of JSON data for this reason.<br />

$.getScript()<br />

Loads a JavaScript from the server using a GET HTTP request, and executes it.<br />

$.getScript(url[, success])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!