16.1 Overview of Ajax - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

16.1 Overview of Ajax

Description:

looks up the city and state for the given zip. code and returns them to the form ... We'll use a simple hash of zip codes and names ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 12
Provided by: scie195
Category:
Tags: ajax | codes | overview | zip

less

Transcript and Presenter's Notes

Title: 16.1 Overview of Ajax


1
16.1 Overview of Ajax - Goal of Ajax is to
provide Web-based applications with
responsiveness approaching that of desk-top
applications - Specific kind of Web
applications that benefit from Ajax are those
that have frequent interactions between the
client and the server - Goals are achieved with
two different approaches 1. Client requests
are handled asynchronously 2. Only small
parts of the current document are
updated - Ajax does not use any new programming
languages or markup languages - Client
side JavaScript, XML, XHTML, DOM, CSS -
Server side any (PHP, servlets, ASP.NET, etc.)
- Possibility of Ajax technology came with the
IE5 ActiveX objects, XMLHTML and XmlDocument
- Now, XMLHttpRequest object is used
2
16.2 The Basics of Ajax - Described through a
very simple application - The application
Helps the user fill a form - The form
gathers client information asks for the
zip code before the names of the city and state
- As soon as the zip code is entered, the
application sends a request to the server,
which looks up the city and state for the
given zip code and returns them to the
form - Uses JavaScript to put the city and
state names in the form - Uses PHP
on the server to look up the city and
state - The form - Must reference the
JavaScript code file in its head - Must
register an event handler on the blur event
of the zip code text box ? SHOW popcornA.html
3
  • 16.2 The Basics of Ajax (continued)
  • - Two functions are required by the
    application
  • 1. The blur handler
  • 2. A function to handle the response
  • The Request Phase (The blur handler)
  • - The communication to the server for the
  • asynchronous request must be made through
    the
  • XMLHttpRequest object, so one must be created
  • var xhr new XMLHttpRequest()
  • - When the server receives an asynchronous
  • request, it sends a sequence of notices,
    called

4
16.2 The Basics of Ajax (continued) - The
Request Phase (continued) - Next, the handler
must call the open method of the XHR
object - Parameters to open
1. HTTP method, GET or POST, quoted 2.
The URL of the response document on the
server 3. A Boolean literal to
indicate whether the request is to be
asynchronous (true) or synchronous
(false) - The parameter (the zip code)
must be attached to the URL (because
GET will be used) xhr.open("GET",
"getCityState.php?zip" zip, true)
(getCityState.php is the response document) -
The request is sent with the send method
xhr.send(null) ? SHOW getPlace.js
5
16.2 The Basics of Ajax (continued) - The
Response Document - Well use a simple hash
of zip codes and names of cities and
states, so this will be very simple - The
response data is produced with a print
statement ? SHOW getCityState.php - The
Receiver Phase - A JavaScript function with
no parameters - Fetch the server response
(text), split it into its two parts (city
and state), and set the corresponding
text boxes to those values - The receiver
function must be able to access the XHR
- If it is global, it would be accessible, but
it could be corrupted by simultaneous
requests and responses - The
alternative is to register the actual code of
the receiver, rather than its name
6
16.2 The Basics of Ajax (continued) - The
Receiver Phase (continued) - Actions of the
receiver function 1. Put all actions in
the then clause of a selector that
checks to see if readyState is 4 2. Get the
response value from the responseText
property of the XHR object 3. Split it into
its two parts 4. Set the values of the city
and state text boxes ? SHOW popcornA.js -
Cross-Browser Support - What we have works
with FX2 and IE7, but not IE5 and IE6
- IE5 and IE6 support an ActiveXObject named
Microsoft.XMLHTTP xhr new
ActiveXObject("Microsoft.XMLHTTP") ? SHOW
getPlace2.js
7
16.3 Rails with Ajax - There is a library of
JavaScript utilities named Prototype, which
has much of what is needed - To access it,
place the following in the Rails template
document "prototype" - Problems with using Rails and
Ajax - Not all elements can be modified or
replaced on all browser using only the
Rails tools - A simple solution Use div
elements for content that is to be
replaced using Ajax - Rails implements Ajax as
follows 1. Triggered by a user event or a
timer 2. Data associated with the event is
sent asynchronously to a controller method
(an action handler) on the server using an
XHR 3. The controller action handler performs
some operation and returns text, XML or
XHTML 4. JavaScript on the browser receives
the response and performs the update to
the document
8
16.3 Rails with Ajax (continued) - Example
popcorn form, again - The application is named
popcornA, the controller is named popcorn,
and the controller class is named
PopcornController - The initial action method
(and the view template) are named show_form
? SHOW show_form.html.erb - Rails uses several
different helper methods to trigger the Ajax
process, most commonly link_to_remote and
observe_field - These are JavaScript
functions wrapped in Ruby methods -
observe_field can take many parameters -
First parameter - a literal string with the id of
the control element to watch -
Other parameters appear as the elements of
a hash literal - The name of the
action method url (action
process_form)
9
16.3 Rails with Ajax (continued) - Example
(continued) - More parameters
update - When update is specified,
the controller action given in url
produces the response using the
render method - The text
parameter is passed to render
text is set (with ) to a string value
render text " "
with - A JavaScript expression
giving the first parameter for the
XHR request object e.g.,
"zipvalue" frequency
- The frequency in seconds at which changes
to the specified widget will be
detected - Not setting a frequency
causes an event to be used, instead
of a timer (changed for text boxes
and click for buttons), rather
than time-based observations.
10
16.3 Rails with Ajax (continued) - Example
(continued) complete, success,
failure - To specify the callback
function to be called when the Ajax
operation is completed - Complete example
call to observer_field observe_field("my_button",
update "name" url action
process_form) - Recall that the
controller action method for the
application gets the response, splits it, and
updates the text boxes for city and state
- This code is the content of a
javascript_tag, because it will be in a
view template, which is an html.erb
file update_city_state(xhr) result
xhr.responseText.split(', ')
document.getElementById('city').value
result0 document.getElementById('state')
.value result1 END
11
16.3 Rails with Ajax (continued) - Example
(continued) - Because this application
updates two elements, update is not used
rather, the name of the update function is
given with complete url action fill_city_state,
complete 'update_city_state(request) ',
with 'zip' ) - The
Controller - The action method,
fill_city_state, generates the city and
state for the form, given the zip code
as a POST parameter ? SHOW popcorn_controller.rb
- In a Rails application, the new partial
document could be produced by either the
controller method or in the template
document - If the template does it, the
action method of the controller must
include render(layout false) - If
the action method does it, it uses render
Write a Comment
User Comments (0)
About PowerShow.com