Docsity

XML HTTP Request Object - Web Development II | INLS 672, Study notes of School management&administration

Partial preview of the text

Download XML HTTP Request Object - Web Development II | INLS 672 and more Study notes School management&administration in PDF only on Docsity!

Before we get started

Tonight, an optional lab

Groups?

Other Questions?

Ajax

Using XMLHttpRequest

  • Create the object (in most current browsers thusly)

and bind to a variable

  • http_request = new XMLHttpRequest();
  • With IE 5 and 6, use:
  • http_request = new

ActiveXObject("Microsoft.XMLHTTP");

  • Create a link between the event handler and a

function that handles the results

  • http_request.onreadystatechange = a function you define ;
  • Create a connection and send the request
  • http_request.open("GET", url, true);
  • http_request.send(null);

Examples from: http://developer.

Examples from: http://developer. mozilla

mozilla .org/en/docs/AJAX:Getting_Started

.org/en/docs/AJAX:Getting_Started

Distinguishing Browsers

• Since this isn't a standard, IE 5/6 and

Mozilla differ in implementing

XMLHttpRequest

• Fortunately, you can test for this easily

if (window. if (window.XMLHttpRequestXMLHttpRequest) { //) { // MozillaMozilla, Safari, ..., Safari, ...

http_request = new http_request = new XMLHttpRequestXMLHttpRequest();();

}

}

} else if (window.

} else if (window. ActiveXObject

ActiveXObject ) { // IE

) { // IE

try {

try {

http_request = new

http_request = new ActiveXObject

ActiveXObject ("Msxml2.XMLHTTP");

("Msxml2.XMLHTTP");

} catch (e) { } catch (e) {

try {

try {

http_request = new

http_request = new ActiveXObject

ActiveXObject ("Microsoft.XMLHTTP");

("Microsoft.XMLHTTP");

} catch (e) {}

} catch (e) {}

}

}

}

}

example from http://developer.

example from http://developer. mozilla

mozilla .org/en/docs/AJAX:Getting_Started

.org/en/docs/AJAX:Getting_Started

The alertContents function

function function alertContentsalertContents(http_request) {(http_request) {

if (http_request.

if (http_request. readyState

readyState == 4) {

== 4) {

if (http_request.status == 200) {

if (http_request.status == 200) {

alert(http_request. alert(http_request.responseTextresponseText););

} else { } else {

alert('There was a problem with the request.');

alert('There was a problem with the request.');

}

}

}

}

} }

The Trigger

• Finally, a trigger to call the function

<span

<span

style="cursor: pointer; text-decoration: underline"

style="cursor: pointer; text-decoration: underline"

onclick="makeRequest

onclick="makeRequest

('test.html')">

('test.html')">

Make a request

Make a request

Jah: Creating the object

function

function jah

jah (

url

url ,target) {

,target) {

if (window.

if (window. XMLHttpRequest

XMLHttpRequest ) {

req

req = new

= new XMLHttpRequest

XMLHttpRequest ();

req

req .

onreadystatechange

onreadystatechange = function() {

= function() { jahDone

jahDone (target);};

(target);};

req

req .open("GET",

.open("GET", url

url , true);

, true);

req

req .send(null);

.send(null);

// IE/Windows ActiveX version

// IE/Windows ActiveX version

} else if (window.

} else if (window. ActiveXObject

ActiveXObject ) {

req

req = new

= new ActiveXObject

ActiveXObject ("Microsoft.XMLHTTP");

("Microsoft.XMLHTTP");

if (

if ( req

req ) {

req

req .

onreadystatechange

onreadystatechange = function() {

= function() { jahDone

jahDone (target);};

(target);};

req

req .open("GET",

.open("GET", url

url , true);

, true);

req

req .send();

.send();

Jah: What's called by the

listener

function

function

jahDone

jahDone

(target) {

(target) {

// only if// only if reqreq is "loaded"is "loaded"

if (

if (

req

req

readyState

readyState

// only if "OK"

// only if "OK"

if ( if (reqreq.status == 200) {.status == 200) {

results =

results =

req

req

responseText

responseText

document. document.getElementByIdgetElementById(target).(target).innerHTMLinnerHTML = results;= results;

} else {

} else {

document.

document.

getElementById

getElementById

(target).

(target).

innerHTML="jah

innerHTML="jah

error:

error:

\n"

\n"

req req..statusTextstatusText;;

Notice that the only thing this looks

Notice that the only thing this looks

for is a state of 4 and a status of 200

for is a state of 4 and a status of 200

You can see this with telnet

gilgamesh:~ hays$ telnet www.cs.unc.edu 80

Trying 152.2.131.244...

Connected to www.cs.unc.edu.

Escape character is '^]'.

GET /indse.htm HTTP/1.

HTTP/1.1 404 Not Found

Date: Wed, 17 Sep 2008 11:41:59 GMT

Server: Apache/2.2.3 (Red Hat)

Content-Length: 287

Connection: close

Content-Type: text/html; charset=iso-8859-

Walking through the steps

• This example shows how the status of

the connection changes

• Notice how a 404 is dealt with, versus

how a security issue is dealt with

XMLHttpRequestWalker2.html

Gotchas

• This has to run through a server (the

XMLHttpRequest object has no access

to files)

• In this version, if the text pulled in isn't

XML, you get an error (but it works)

– This is due to the forcing of the type of the

XMLHttpRequest results

– Which begs a question--do you need to

use XML?

– And if not XML, what?

innerHTML.html

• All of these examples use the

innerHTML property….

• Did I show you this?

• This shows some simple manipulations

of a page using the innerHTML property

outside of ajax

• Note that pictures don't have any

innerHTML (since there's no HTML

there)

The Logic

• checkid.php takes a GET variable, get_id,

and checks that against a text file list of ids

• form_contents1_02.html has a form with a

box

  • onchange is the trigger, calls an ajax function

(trigger this with a tab, a return submits the form!)

  • that function passes the content to checkid.php

and returns it's results to a div tag (or an alert)

  • thus it dynamically checks the validity without a

reload

• This you can do with either innerHTML or

DOM references

checkid.php

$ids=file(".
$ids=file(".
/files/userids
/files/userids
.txt");
.txt");
$x=0 $x=0;;
while ($ids[ while ($ids[$x$x])])
$ids[
$ids[
$x
$x
] = trim($ids[
] = trim($ids[
$x
$x
]);
]);
$x$x++;++;
if (
if (
strlen
strlen
($_GET[
($_GET[
"get_id"
"get_id"
]) < 3 )
]) < 3 )
echo
echo
"Userids
"Userids
must be 3 or more characters";
must be 3 or more characters";
elseif
elseif
strlen
strlen
($_GET[
($_GET[
"get_id"
"get_id"
]) > 10 )
]) > 10 )
echo
echo
"Userids
"Userids
cannot be more than 10 characters";
cannot be more than 10 characters";
else
else
echo "Thisecho "This useriduserid is available!";is available!";