Contents. Demos folder: Demos\14-Ajax. 1. Overview of Ajax. 2. Using Ajax directly. 3. jquery and Ajax. 4. Consuming RESTful services

Similar documents
An Introduction to AJAX. By : I. Moamin Abughazaleh

Ajax. Ronald J. Glotzbach


COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

XMLHttpRequest. CS144: Web Applications

Ajax. David Matuszek's presentation,

20486-Developing ASP.NET MVC 4 Web Applications


Jquery Ajax Json Php Mysql Data Entry Example

CSC Javascript

REST - Representational State Transfer

REST AND AJAX. Introduction. Module 13

AJAX: Introduction CISC 282 November 27, 2018

2/6/2012. Rich Internet Applications. What is Ajax? Defining AJAX. Asynchronous JavaScript and XML Term coined in 2005 by Jesse James Garrett

Part of this connection identifies how the response can / should be provided to the client code via the use of a callback routine.

TIME SCHEDULE MODULE TOPICS PERIODS. HTML Document Object Model (DOM) and javascript Object Notation (JSON)

Developing ASP.NET MVC 5 Web Applications

AJAX. Introduction. AJAX: Asynchronous JavaScript and XML

Working with Javascript Building Responsive Library apps

AJAX Programming Chris Seddon

Session 18. jquery - Ajax. Reference. Tutorials. jquery Methods. Session 18 jquery and Ajax 10/31/ Robert Kelly,

Session 11. Ajax. Reading & Reference

Course 20486B: Developing ASP.NET MVC 4 Web Applications

Developing ASP.NET MVC 4 Web Applications

A.A. 2008/09. What is Ajax?

CITS1231 Web Technologies. Ajax and Web 2.0 Turning clunky website into interactive mashups

20486: Developing ASP.NET MVC 4 Web Applications

Web Application Security

CS 5142 Scripting Languages

COURSE 20486B: DEVELOPING ASP.NET MVC 4 WEB APPLICATIONS

20486: Developing ASP.NET MVC 4 Web Applications (5 Days)

Creating an Online Catalogue Search for CD Collection with AJAX, XML, and PHP Using a Relational Database Server on WAMP/LAMP Server

CSC 443: Web Programming

Developing ASP.NET MVC 4 Web Applications

Web Programming Step by Step

IN Development in Platform Ecosystems Lecture 3: json, ajax, APIs

Visual Studio Course Developing ASP.NET MVC 5 Web Applications

Programming for Digital Media. Lecture 7 JavaScript By: A. Mousavi and P. Broomhead SERG, School of Engineering Design, Brunel University, UK

ASP.NET MVC Training

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 07 Minor Subject

Web Development 20480: Programming in HTML5 with JavaScript and CSS3. Upcoming Dates. Course Description. Course Outline

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Course 20480: Programming in HTML5 with JavaScript and CSS3

HTTP Communication on Tizen

Developing ASP.Net MVC 4 Web Application

COURSE 20480B: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

JAVASCRIPT JQUERY AJAX FILE UPLOAD STACK OVERFLOW

Oracle Service Cloud Integration for Developers Ed 1

Programming in HTML5 with JavaScript and CSS3

Developing ASP.NET MVC 4 Web Applications

AJAX: Rich Internet Applications

AJAX: The Basics CISC 282 March 25, 2014

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Introduction to Ajax

Discussion #4 CSS VS XSLT. Multiple stylesheet types with cascading priorities. One stylesheet type

Jquery.ajax Call Returns Status Code Of 200 But Fires Jquery Error

Controller/server communication

User Interaction: jquery

jquery and AJAX

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

JavaScript Programming

Controller/server communication

Programming in HTML5 with JavaScript and CSS3

Developing ASP.NET MVC 5 Web Applications. Course Outline

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

CNIT 129S: Securing Web Applications. Ch 3: Web Application Technologies

Portlets and Ajax: Building More Dynamic Web Apps

CSCI 6312 Advanced Internet Programming

Course Details. Skills Gained. Who Can Benefit. Prerequisites. View Online URL:

Tooling for Ajax-Based Development. Craig R. McClanahan Senior Staff Engineer Sun Microsystems, Inc.

Lesson 14 SOA with REST (Part I)

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

Oracle Service Cloud Integration for Developers Ed 1

AJAX: The Basics CISC 282 November 22, 2017

Developing ASP.NET MVC 5 Web Applications

AJAX(Asynchronous Javascript + XML) Creating client-side dynamic Web pages

Session 12. RESTful Services. Lecture Objectives

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

CSC 309 The Big Picture

AJAX and PHP AJAX. Christian Wenz,

Outline. AJAX for Libraries. Jason A. Clark Head of Digital Access and Web Services Montana State University Libraries

Ten good practices for ASP.NET MVC applications

Ajax- XMLHttpResponse. Returns a value such as ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, based on the value of

20486 Developing ASP.NET MVC 5 Web Applications

Lesson 12: JavaScript and AJAX

Web Application with AJAX. Kateb, Faris; Ahmed, Mohammed; Alzahrani, Omar. University of Colorado, Colorado Springs

CL_55244 JavaScript for Developers

Asynchronous JavaScript + XML (Ajax)

Cleveland State University Department of Electrical and Computer Engineering. CIS 408: Internet Computing

,

Other architectures are externally built or expanded

Notes General. IS 651: Distributed Systems 1

Term Paper. P r o f. D r. E d u a r d H e i n d l. H o c h s c h u l e F u r t w a n g e n U n i v e r s i t y. P r e s e n t e d T o :

Module7: AJAX. Click, wait, and refresh user interaction. Synchronous request/response communication model. Page-driven: Workflow is based on pages

Introduction to REST Web Services

Web Site Development with HTML/JavaScrip

CS50 Quiz Review. November 13, 2017

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

AJAX Workshop. Karen A. Coombs University of Houston Libraries Jason A. Clark Montana State University Libraries

Abstract. 1. Introduction. 2. AJAX overview

Transcription:

Ajax

Contents 1. Overview of Ajax 2. Using Ajax directly 3. jquery and Ajax 4. Consuming RESTful services Demos folder: Demos\14-Ajax 2

1. Overview of Ajax What is Ajax? Traditional Web applications Ajax Web applications Uses of Ajax Advantages of Ajax 3

What is Ajax? Ajax = "Asynchronous JavaScript with XML" Ajax is a collection of technologies HTML and Cascading Style Sheets (CSS) Web presentation Document Object Model (DOM) and JavaScript Client-side dynamic behaviour XML and XSLT Data exchange, transformation, and manipulation XMLHttpRequest A JavaScript object that performs asynchronous data retrieval Available on most mainstream browsers JavaScript Object Notation (JSON) String representation of an object, easy to handle in client-side JavaScript code 4

Traditional Web Applications HTTP request Browser UI Web server HTTP response HTML and CSS data Client Server(s) 5

Ajax Web Applications JavaScript HTTP request Browser UI Ajax engine Web server HTML and CSS data HTTP response E.g. XML or JSON Client Server(s) 6

Uses of Ajax Some benefits of Ajax: Validate data in real time Auto-complete data entry Load data in the background Refresh data asynchronously Portlets 7

Advantages of Ajax Bandwidth usage Ajax web pages load relatively quickly, because they generate HTML locally within the browser Improved user interface Ajax pages feel more like a standalone app than a Web page Separation of data representation vs. presentation Separate the data from the formatting and styling 8

2. Using Ajax Directly Sample application scenario Defining the Web page UI Creating an XMLHttpRequest object Making asynchronous calls Implementing the server functionality Implementing the call-back function 9

Sample Application Scenario Let's see how to implement a simple Ajax Web app... HTML form at the client Web Server UI components 1 JavaScript UI event handlers 2 RESTful Web service generates and returns XML data 4 JavaScript callback function 3 Example: Open AjaxDemo.sln in Visual Studio 10

Defining the Web Page UI <html> <head> <script type="text/javascript"> See following slide </script> </head> <body> <input type="text" id="myinputfield" onkeyup="myonkeyup()" /> <p id="myechofield" /> </body> </html> /Views/Home/UseAjaxDirectly.cshtml 11

Creating an XMLHttpRequest Object The following JavaScript code creates an XMLHttpRequest object when the HTML page is loaded <script type="text/javascript"> var request = mycreatexmlhttprequest(); function mycreatexmlhttprequest() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} try { return new XMLHttpRequest(); } catch (e) {} return null; } </script> /Views/Home/UseAjaxDirectly.cshtml 12

Making Asynchronous Calls Here's the myonkeyup() function Passes user's text asynchronously to an ASP.NET MVC Web app At the server, it invokes Echo() on the HomeController class function myonkeyup() { if (request!= null) { var textfield = document.getelementbyid("myinputfield"); var url = "/Home/Echo?text=" + textfield.value; request.open("get", url, true); request.onreadystatechange = myhandlecallback; request.send(null); } } /Views/Home/UseAjaxDirectly.cshtml 13

Implementing the Server Functionality We have implemented the server functionality as an action method in an ASP.NET MVC controller class The method simple returns the same text immediately back to the client (i.e. "echo" ) public class HomeController : Controller { public string Echo(string text) { return text; } } /Controllers/HomeController.cs 14

Implementing the Call-Back Function Here's the client-side JavaScript call-back function Retrieves the response from the server, and updates the UI Note: To retrieve a text response, use the responsetext property To retrieve an XML response, use the responsexml property function myhandlecallback() { if (request.readystate == 4 && request.status == 200) { var outputfield = document.getelementbyid("myechofield"); outputfield.innerhtml = request.responsetext; } } /Views/Home/UseAjaxDirectly.cshtml 15

3. jquery and Ajax Making an Ajax request using Ajax Additional possibilities Setting default values for Ajax requests Using deferred syntax 16

Making an Ajax Request using Ajac To make an Ajax HTTP request to a Web server URL: $.get( url, [ data ], or $.post [ success(data, textstatus, jqueryxmlhttprequestobject) ], [ datatype ] ) 17

Setting Default Values for Ajax Requests You might find that a lot of your Ajax requests require the same parameter settings E.g. cache, datatype, etc. You can use $.ajaxsetup() to set default options for all subsequent Ajax requests E.g. disable caching E.g. specify you want all data to be returned as JSON $.ajaxsetup({ cache: false, datatype: "json" }); 18

Additional Possibilities Generalized Ajax call: $.ajax({ type: "GET", url: "url", data: somedata, success: callback, error: callback }); Example: See UseAjaxWithJQuery.cshtml 19

Using Deferred Syntax jquery also supports a "deferred" syntax as follows: $.ajax({ type: "GET", url: "url", cache: false, data: somedata }).done(function(data) { // success code }).fail(function (data) { // error code }).always(function (data) { // unconditional code }); Example: See UseAjaxWithJQueryDeferred.cshtml 20

Lab A Using Ajax with jquery Lab doc: 14a Using Ajax with jquery.docx Lab exercises: 1. Understanding the application structure 2. Making an Ajax call 3. Processing the Ajax response 4. Additional suggestions 21

4. Consuming RESTful Services The name "REST" What is a RESTful service? HTTP verbs HTTP response codes Example scenario Calling RESTful services using jquery 22

The Name "REST" The name Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: a network of Web pages forms a virtual state machine, allowing a user to progress through the application by selecting a link or submitting a short data-entry form, with each action resulting in a transition to the next state of the application by transferring a representation of that state to the user. Fielding & Taylor 2002 23

What is a RESTful Service? RESTful services are resource-centric services Endpoints (URIs) represent resources Endpoints are accessible via standard HTTP Endpoints can be represented in a variety of formats (e.g. XML, JSON, HTML, plain text) 24

HTTP Verbs RESTful services use HTTP verbs to define CRUD-style operations on resources HTTP verb POST GET PUT DELETE Meaning in CRUD terms Create a new resource from the request data Read a resource Update a resource from the request data Delete a resource 25

HTTP Response Codes RESTful services return data, and set a response code to indicate the outcome HTTP response code Official HTTP meaning RESTful meaning 200 OK Request OK 201 Created New resource created OK 400 Bad request 403 Forbidden 404 Not found 405 Method not allowed 410 Resource gone 415 Unsupported media type Request malformed Request refused Resource not found Method not supported Can't update or delete item Content type not recognized 500 Internal server error Request processing failed 26

Example Scenario: Overview The easiest way to understand RESTful services is through an example... Imagine a hypothetical company that sells many different parts to many customers The company wants to provide the following Web services: Get list of parts Get detailed part information Submit purchase orders (PO) 27

Example Scenario: Getting a Part List The list of parts available from the company is a resource: A resource is identified via a URI Resource representations are available via HTTP GET E.g. GET http://www.acme.com/parts HTTP/1.1 Gets a representation of the "list of parts" resource <?xml version="1.0"?> <p:parts xmlns:p="http://www.acme.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part id="0345" xl:href="http://www.acme.com/parts/0345"/> <Part id="0346" xl:href="http://www.acme.com/parts/0346"/> <Part id="0347" xl:href="http://www.acme.com/parts/0347"/> <Part id="0348" xl:href="http://www.acme.com/parts/0348"/> </p:parts> A representation typically contains links to other resources 28

Example Scenario: Getting Part Info Detailed part info is also a resource identified with a URL Getting detailed information: E.g. GET http://www.acme.com/parts/0346 HTTP/1.1 Gets a representation of a specific part <?xml version="1.0"?> <p:part xmlns:p="http://www.acme.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part-ID>0346</Part-ID> <Name>Widget-A</Name> <Desc>This part is used in the pilot's cabin</desc> <Spec xl:href="http://www.acme.com/parts/0346/spec"/> <UnitCost currency="usd">0.10</unitcost> </p:part> How the representation is generated is unspecified Note: the URIs are logical URIs, not necessarily static pages 29

Example Scenario: Submitting a PO To order a part, the client submits a purchase order (PO) The PO is an XML document The PO is submitted using standard HTTP POST <?xml version="1.0"?> <p:order xmlns:p="http://www.acme.com" xmlns:xl="http://www.w3.org/1999/xlink"> <Part xl:href="http://www.acme.com/parts/0346"/> <Quantity>10</Quantity> <Date>2014-09-01</Date>... </p:order> A PO is shared information between the company and client The Web service returns a URI for the submitted PO The client may delete the PO using HTTP DELETE 30

Calling RESTful services using jquery (1 of 4) To issue a GET request: $.ajax({ type: "GET", url: urlofitemtoget, cache: false, accepts: { json: 'application/json' }, success: function(data) { }, error: }); function(xhr, message) { } 31

Calling RESTful services using jquery (2 of 4) To issue a POST request: $.ajax({ type: url: cache: "POST", aurl, false, contenttype: "application/json", data: JSON.stringify(anObject), success: function(data) { }, error: }); function(xhr, message) { } 32

Calling RESTful services using jquery (3 of 4) To issue a PUT request: $.ajax({ type: url: cache: "PUT", aurl, false, contenttype: "application/json", data: JSON.stringify(anObject), success: function() { }, error: }); function(xhr, message) { } 33

Calling RESTful services using jquery (4 of 4) To issue a DELETE request: $.ajax({ type: url: cache: "DELETE", urlofitemtodelete, false, success: function() { }, error: }); function(xhr, message) { } 34

Lab B Consuming RESTful Services Lab doc: 14b Consuming RESTful Services using jquery and Ajax.docx Lab exercises: 1. Familiarization with the server-side code 2. Familiarization with the client-side code 3. Inserting a product 4. Updating a product 5. Deleting a product 6. Additional suggestions 35

Any Questions? 36