ajax · introduction ajax stands for asynchronous javascript and xml. ajax is a group of...

27
Group 1 Pham Thanh Dat Nguyen Hung Cuong Bui Thanh Tung Tran Thi Thu Phuong Nguyen Anh Dung AJAX

Upload: others

Post on 16-Aug-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Group 1

Pham Thanh Dat

Nguyen Hung Cuong

Bui Thanh Tung

Tran Thi Thu Phuong

Nguyen Anh Dung

AJAX

Page 2: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Content

History of AJAX

Introduction

Technologies Used

Demo

Conclusion

Page 3: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

History of Ajax

• Static web pages

– Static html page is loaded

– No interaction with user

• Dynamic web pages

– CGI, Applet (5/1995), JavaScript, Servlet and ASP, Flash (1996)

– IFFRAME (1996) and Layer (1997): asynchronous; Remote Scripting (1998)

– In 2005 Google use XMLHttpRequest, the name Ajax started to be so popular.

Page 4: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Purpose of AJAX

Prevents unnecessary reloading of a page.

AJAX aims at loading only the necessary information, and making only the necessary changes on the current page without reloading the whole page.

Page 5: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Introduction

AJAX stands for Asynchronous JavaScript and XML.

Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications.

Web applications can retrieve data from the server asynchronously without interfering with the display and behavior of the existing page.

Data is usually retrieved using the XMLHttpRequest

object.

Ajax is a group of technologies: HTML, CSS, DOM, XML, JSON, JavaScript, ...

Page 6: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

What supports and not support

Support:

Konqueror, Internet Explorer 4.0 and upper, Mozilla Firefox...

Not support: mobile explorer

Tools for AJAX:

Atlas, toolkit Ajax (Microsoft )

Dojo Toolkit, toolkit Ajax/DHTML

Prototype

Sajax, toolkit Ajax

Rialto (Rich Internet AppLication TOolkit)

Page 7: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Technology used

Javascript and other script client (client side)

XML (for information exchange)

ASP or JSP, PHP, .NET… (server side)

Page 8: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Why Ajax

Page 9: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

XMLHttpRequest : Method

XMLHttpRequest used to send a request and receive

a response.

Method:

open(HttpRequest, url, aschyn); send(contend);

setsetRequestHeader(string header, string value),

abort(); getAllResponseHeaders();

getResponseHeader( String header)

Page 10: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

XMLHttpRequest: Properties

• onreadystatechange

• readyState: 0 (Uninitialized) request not initialized

1 (Loading) server connection established

2 (Loaded) request received

3 (Interactive) processing request

4 (Complete): request finished and response is ready

• responseText:

• responseXML.

• Status: 200: OK; 400: not found

• statusText: OK or Not Found, …

Page 11: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

How the Ajax work with XMLHTTPRequest

Web page Ajax Engine

Server

Request

readyState

response

Process

Return

data

XMLHTTPRequest

Create serverrequest

send

Monitor status

Get response

Capture

event

Updatepage

Page 12: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Create an XMLHttpRequest Object

if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera,

Safarixmlhttp=new XMLHttpRequest();}

else{// code for IE6, IE5xmlhttp=new

ActiveXObject("Microsoft.XMLHTTP");}

Page 13: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Send a Request To a Server

• Method open(method,url,async)

– method: the type of request: GET or POSTurl: the location of the file on the serverasync: true (asynchronous) or false (synchronous)

• Method send(string)

– string: Only used for POST requests

Example:

xmlhttp.open("GET","demo_get.asp",true);xmlhttp.send();

Page 14: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

GET or POST?

• GET is simpler and faster than POST

• Use POST requests when:

– A cached file is not an option (update a file or database on the server)

– Sending a large amount of data to the server (POST has no size limitations)

– Sending user input (which can contain unknown characters), POST is more robust and secure than GET

Page 15: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

POST example

xmlhttp.open("POST","ajax_test.asp",true);

xmlhttp.setRequestHeader("Content-

type","application/x-www-form-urlencoded");

xmlhttp.send("fname=Henry&lname=Ford");

Page 16: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

setRequestHeader(header,value)

• Adds HTTP headers to the request.

header: specifies the header namevalue: specifies the header value

Page 17: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Server Response

• ResponseText Property

• ResponseXML Property

Page 18: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

XMLHTTPRequest : Example Send Response

Document.getElementById("myDiv").innerHTML=xmlhttp.responseText

xmlDoc=xmlhttp.responseXML;var txt="";x=xmlDoc.getElementsByTagName("ARTIST");for (i=0;i<x.length;i++){txt=txt + x[i].childNodes[0].nodeValue + "<br />";}

document.getElementById("myDiv").innerHTML=txt;

Page 19: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

XMLHTTPRequest : Example onreadystatechange

xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 &&

xmlhttp.status==200){document.getElementById("myDiv").innerH

TML=xmlhttp.responseText;}

}

Page 20: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Conclusion

Advantages:

Web page can be refreshed dynamically and load much faster.

Disadvantages:

• The back button may be deactivated.

• Hard to bookmark Ajax web pages.

• Requires a stable (and fast) internet connetion.

• Ajax pages are not indexable by web crawlers.

Page 21: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications
Page 22: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Does AJAX requires JavaScript? Explain in details

• No, It does not.

• Explain:

AJAX requires Client-side script, you can choice JavaScript or VBScript.

AJAX usually comes with JavaScript because it's being supported by almost browsers, but VBScript only can run in Internet Explorer.

Page 23: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Explain more in details the notion of "state" for the XMLHttpRequest object. What is the purpose of this "state".

• State or readyState is the current state of the request operation. ReadyState can be one of the following values:0: The object has been created, but not initialized (the open method has not been called).1: A request has been opened, but the send method has not been called. 2: The send method has been called. No data is available yet. 3: Some data has been received; however, neither responseText nor responseBody is available. 4: All the data has been received.

Page 24: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Example of situations where it is better to do GET requests

Use GET requests is simpler and faster than POST requests, but GET request is less secure and has limitation in the length of parameters.Situations:- Use GET requests when you pass simple parameters to the target pages.Example:xmlhttp.open("GET","get_ticket_status.asp?mode=1&cinema=mega_star&movie=basic_instinct",true);xmlhttp.send();

Page 25: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

Example of situations where it is better to perform POST requests.

Use POST requests when you need to submit a large amount of data to target pages, or send authentication data.Example:xmlhttp.open("POST","ajax_post.asp",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");var email_body = encodeURIComponent(document.form1.email_body);xmlhttp.send("email_body="+email_body);

Page 26: AJAX · Introduction AJAX stands for Asynchronous JavaScript and XML. Ajax is a group of interrelated web development methods used on the client-side to create interactive web applications

The response sent by the server is always in XML. True or false? Explain.

• It is False.

• Explain:

The response format sent by the server depends on the response body format and the way you receive it.If the response format is plain text or HTML, you can use XMLHTTP.responseText to get the response body as a string.If the response format is XML, you can use XMLHTTP.responseXML to retrieves the response body as an XML Document Object Model (DOM) object