Introduction to AJAX

31
Introduction to AJAX & jQuery Jon Tedesco 1

Transcript of Introduction to AJAX

Page 1: Introduction to AJAX

1

Introduction to AJAX & jQueryJon Tedesco

Page 2: Introduction to AJAX

2

Outline

Web Requests GET &POST

AJAX Basics

JavaScript Overview Callbacks AJAX

jQuery Features

DemoJon Tedesco

Page 3: Introduction to AJAX

3

Web RequestsGET & POST, Client & Server Perspectives

Jon Tedesco

Page 4: Introduction to AJAX

4

Server

(Google)

A Basic Web Request

Client (Browser) Submit single web

request Receive & render

HTML

Server Receive request Return HTML

Static One request, no

further interaction

Client

(Browser)

User enters http://google.com

Server Responds

with HTML

Tim

e

Jon Tedesco

Page 5: Introduction to AJAX

5

GET & POST: Summary

GET

Retrieve data

Display data in URL

Example Fetching a web page

POST

Modify or submit data

Data hidden from URL

Example Submitting a form

Either allow passing parameters How does each behave? When should we use one over

the other?

Jon Tedesco

Page 6: Introduction to AJAX

6

Server

(Google)

A Basic Web Request

GET Most basic request Sent by browser for

fetching a page

POST Often used for form

data

Why? Simple Fast Idempotent

Client

(Browser)

GET: http://google.com

Server Responds

with HTML

Tim

e

Jon Tedesco

Page 7: Introduction to AJAX

7

GET & POST: Characteristics

GET

More usable GET requests can be

cached GET requests can remain in

the browser history GET requests can be

bookmarked GET requests can be

distributed & shared

URL length Prohibitive for large

requests

POST

More complex Slower

Secure (sort of) Parameters are not

shown in URL

Better for large amounts of data

Not sharable

Jon Tedesco

Page 8: Introduction to AJAX

8

GET & POST: Best Practices

GET

Use for idempotent actions

Use for AJAX Fast

Use for sharing links

Example YouTube Google

POST

Use for actions with side effects Potential modify or

delete

Use for large data

Use for sensitive data

Example Comments

Jon Tedesco

Page 9: Introduction to AJAX

9

GET & POST: From PHP

GET POST

Jon Tedesco

myserver/endpoint.php?foo=1&bar=word myserver/endpoint.php

Received as associative array in PHP

Consider characteristics of each

Page 10: Introduction to AJAX

10

AJAXA high-level introduction

Jon Tedesco

Page 11: Introduction to AJAX

AJAX: Overview

What is it?

Use JavaScript to send web requests after page load

Asynchronous JavaScript And XML

Why use it?

Make site more interactive

Update content without reloading

Example

Facebook Ticker

Jon Tedesco

Page 12: Introduction to AJAX

12

Server

(Google)

AJAX: Diagram

Client (Browser) Submit request No page reload Handle response

Server (Google) Receive request Respond with some data

HTML, JSON, XML, etc.

Simply GET or POST requests Parameter data Receive & handle response

from server

How do we do this? JavaScript

Client

(Browser)

User enters http://google.com

Server Responds

with HTML

Tim

e

Jon Tedesco

User starts typing

Google Instant

Keyword suggestions

Page 13: Introduction to AJAX

13

JavaScriptKey features & use with AJAX

Jon Tedesco

Page 14: Introduction to AJAX

14

JavaScript: Introduction

Runs on user’s browser

Place in <script> … </script>

Run on page load

Basic language properties

Interpreted

Single-threaded

Jon Tedesco

Page 15: Introduction to AJAX

15

JavaScript: Basics

Language

Basic, readable

syntax

Automatic type

casting

Arrays, dictionaries

Development

Console Demo

Jon Tedesco

Page 16: Introduction to AJAX

16

JavaScript: Functions

Functions

First-class citizens

Associated with

scope

Callbacks

JavaScript is built for

asynchronous

programming!

Jon Tedesco

Page 17: Introduction to AJAX

17

JavaScript: Browser Example

Jon Tedesco

Page 18: Introduction to AJAX

18

JavaScript: AJAX Example

Trigger JavaScript On page load On user event On a timer …

Submit HTTP request

Handle server response Callbacks

Jon Tedesco

Page 19: Introduction to AJAX

19

JavaScript: AJAX Example

PHP (endpoint.php)

Jon Tedesco

JavaScript (triggered by event)

Page 20: Introduction to AJAX

20

jQueryKey features & use with AJAX

Jon Tedesco

Page 21: Introduction to AJAX

21

jQuery: Motivation

JavaScript can be challenging

Too much code for simple tasks

Browser inconsistency

jQuery simplifies common tasks

DOM traversal & manipulation

Event handling

Animations

AJAX

Jon Tedesco

Page 22: Introduction to AJAX

22

jQuery: Motivation

jQuery is …

Fast

Concise

Lightweight

Multi-browser compatible

Widely used

Over 55% of 10,000 most-visited sites

Over half of all websites

Jon Tedesco

Page 23: Introduction to AJAX

23

jQuery: Basic Selectors

Without jQuery With jQuery

DOM selection differs across browsers

Using jQuery, you can easily handle element selections Hide/show Animate Modify content …

Jon Tedesco

Page 24: Introduction to AJAX

24

jQuery: DOM Manipulation & Animation

Without jQuery With jQuery

Jon Tedesco

Page 25: Introduction to AJAX

25

jQuery: AJAX

Without jQuery With jQuery

Jon Tedesco

Page 26: Introduction to AJAX

26

AJAX Demo

Let’s write the code…

PHP endpoint

Empty web page

Tools

jQuery

PHP

Google Chrome console

Jon Tedesco

Page 27: Introduction to AJAX

27

AJAX Demo

Jon Tedesco

index.html

Console / JavaScript

endpoint.php

Page 28: Introduction to AJAX

28

AJAX Demo

Jon Tedesco

Console / JavaScript

endpoint.php

index.html

Page 29: Introduction to AJAX

29

Summary

Jon Tedesco

Web Requests GET &POST

AJAX Basics

JavaScript Overview Callbacks AJAX

jQuery Features

Demo

Page 30: Introduction to AJAX

30

Questions?Jon Tedesco

Page 31: Introduction to AJAX

31

References

GET v. POST http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post#disqus_thread http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-us

e-get

http://www.jquery4u.com/ajax/key-differences-post/#.UIoD0Gl26QY

AJAX http://scorebig.files.wordpress.com/2011/09/feed1.jpg http://www.w3schools.com/ajax/ajax_intro.asp

JavaScript http://en.wikipedia.org/wiki/JavaScript

jQuery http://en.wikipedia.org/wiki/JQuery http://www.sitepoint.com/jquery-used-on-50-percent-of-websites/ http://www.slideshare.net/arwid/jquery-4470993

Jon Tedesco