javascript pocket guide [burdette]

Page 278

Chapter 15: Ajax

251

“Loading …” until the Ajax request finishes (\u2026 is the Unicode escape sequence for an ellipsis). function loadingFunc() { var message = $("<div/>", { className : "message" }).text("Loading \u2026"); table.find("caption").append(message); }

Error Handling If the user were to point the browser to a missing page, it would show them an error message. If an Ajax request fails, the browser doesn’t inform the user, so you’ll have to do it for them. The errorFunc() function changes the “Loading …” message to an error message. function errorFunc() { $(".message").text("Error loading data."); }

Handling the Response The last Ajax callback function handles a successful response and updates the page accordingly. function successFunc(response) { table.find(".message").remove(); insertJson(response.people); updatePageLinks(+response.page); // Convert string to ➥ number with + }


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.