HTML and CSS Reference
In-Depth Information
The AJAX call has a few important parameters that you can set. Look at the AJAX call from
Listing 2-2:
$.ajax({
url: searchPath,
cache: false,
dataType: "xml",
success: function (data) {
$(data).find("fruit").each(
function () {
$('#searchResults').append($(this).text());
$('#searchResults').append("<BR/>");
})
}
});
The first parameter being set is the url that the AJAX call will be requesting. For security
reasons, to prevent cross-site scripting, this URL must be within the same domain as the web-
page itself.
The next parameter, cache , is optional and indicates whether the call can use a cached
copy. The third parameter, datatype , indicates the expected data type, which could be XML or
JavaScript Object Notation (JSON), for example.
The last parameter set in this example is the success property. This parameter takes a func-
tion that the results of the AJAX calls should be passed into for the webpage to do some work
with. In this example, the results are parsed and added to the DOM so that users can see the
results.
Another property that can be set on the AJAX call, as good practice, is the error property
so that any error conditions can be handled gracefully. This is the listing updated with an er-
ror function set:
$.ajax({
url: searchPath,
cache: false,
dataType: "xml",
success: function (data) {
$(data).find("fruit").each(
function () {
$('#searchResults').append($(this).text());
$('#searchResults').append("<BR />");
})
},
error: function (xhr, textStatus, errorThrown) {
$('#searchResults').append(errorThrown);
}
});
The error function is passed three useful parameters:
The HTTP request itself
The HTTP error number (such as 404)
The error text (such as Not Found)
 
Search WWH ::




Custom Search