fs-21cs62-MODULE 5
fs-21cs62-MODULE 5
We will be working with the leading Python web framework, Django, on the
Migrations Ajax
server side, and jQuery-powered andonForm Processing
the client side.
Ajax and the XMLHttpRequest object
Ajax is not a technology like JavaScript or CSS, but is more like an overlaid function.
A bare-bones XMLHttpRequest object can be expected to have the following methods and
properties. Migrations and Form Processing
Methods
• The method is GET, POST, HEAD, or one of the other less frequently used
Migrations
methods defined for HTTP.
and Form Processing
• The URL is the relative or absolute URL to fetch.
• As a security measure for JavaScript running in browsers on trusted internal
networks, a same origin policy is in effect, prohibiting direct access to servers
other than one the web page came from.
• The asynchronous variable defaults to true, meaning that the method call
should return quickly in most cases, instead of waiting for the network
operation to complete.
• The last two arguments are the username and password as optionally specified
in HTTP. If they are not specified, they default to any username and password
defined for the web page.
2. XMLHttpRequest.responseText,
Migrations XMLHttpRequest.responseXML
and Form Processing
• While there have been problems encountered with using XMLHttpRequest to
fetch raw binary data, the XMLHttpRequest object is commonly used to fetch
not only XML but XHTML, HTML, plain text, and JSON, among others.
• it would make excellent sense to name it "TextHttpRequest." Once the
request reaches a ready state of 4 ("loaded"), the responseText field will
contain the text that was served up, whether the specific text format is XML
or anything else.
• the responseXML field will hold a parsed XML document
3. XMLHttpRequest.status,
MigrationsXMLHttpRequest.statusText
and Form Processing
• The status field contains the HTTP code, such as 200 for OK;
• the statusText field has a short text description, like OK.
• The callback event handler should ordinarily check XMLHttpRequest.readyState and wait before
acting on server-provided data until the readyState IS 4
• If the server-response has been transmitted successfully, the readyState will be 4 and the status
will be 200.
JavaScript/Ajax Libraries
Migrations and Form Processing
JavaScript libraries offer several advantages. They can reduce chores and
boilerplate code, significantly lessening the pain of JavaScript, and provide a
more uniform interface. They can also provide (for instance) ready-made
widgets; we will be working with a jQuery slider later on in this book. And on a
broad scale, they can let the JavaScript you write be higher-level and a little more
Pythonic.
With the simpler, "virtual high-level language" of jQuery, you accomplish more or less the
same with only:
Migrations and Form Processing
$("#result").load("/project/server.cgi", "text=world");
$() selects a wrapped set. This can be a CSS designation, a single HTML entity referenced
by ID as we do here, or some other things.
Syntax
$.ajax({name:value, name:value, ... })
The parameters specifies one or more name/value pairs for the AJAX request.
Closures
Migrations
The principle at play is and Form Processing
the principle of closures.
Closures represent a key concept in core JavaScript, along with other things such as functions,
objects, and prototypes. Closures are part of the conceptual landscape and not only because
they are the basis on how to effectively create an object with private fields.
query=pizza&page=2
or given as a dictionary, as follows:
{page: 2, query: "pizza"}
dataFilter
This is a reference to a function that will be passed two arguments: the raw response given by an
XMLHttpRequest, and a type (xml, json, script, or html). This offers an important security hook:
JSON can be passed to an eval(), but malicious JavaScript that is passed in place of JSON data can
also be passed to an eval()
type
The type of the request, for example GET, POST, and so on. The default is GET, but this is only
appropriate in very limited cases and it may make sense to simply always use POST.
url
The URL to submit to; this defaults to the current page.
Sample invocation
A sample invocation is as follows: $.ajaxSetup({dataType: "text", type: "POST"});
http://jqueryui.com/themeroller/
When you have made any customizations and downloaded a theme, you can unpack it under
your static content directory