AJAX with jquery. ACM Webmonkeys 2011



Similar documents
Preventing SQL Injection and XSS Attacks. ACM Webmonkeys, 2011

jquery Tutorial for Beginners: Nothing But the Goods

Example. Represent this as XML

Instructions for Embedding a Kudos Display within Your Website

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Webmail Using the Hush Encryption Engine

Building Dynamic Websites With the MVC Pattern. ACM UIUC, 2010

AJAX The Future of Web Development?

It is highly recommended that you are familiar with HTML and JavaScript before attempting this tutorial.

Website Login Integration

MASTERTAG DEVELOPER GUIDE

CMSC434 TUTORIAL #3 HTML CSS JavaScript Jquery Ajax + Google AppEngine Mobile WebApp HTML5

ecommercesoftwareone Advance User s Guide -

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

Project 2: Web Security Pitfalls

Government Girls Polytechnic, Bilaspur

Tracking Campaigns with G-Lock Analytics

Magento 1.4 Theming Cookbook

WEB PROGRAMMING LAB (Common to CSE & IT)

Login with Amazon. Getting Started Guide for Websites. Version 1.0

Software Requirements Specification For Real Estate Web Site

General principles and architecture of Adlib and Adlib API. Petra Otten Manager Customer Support

Working with Indicee Elements

Overview. In the beginning. Issues with Client Side Scripting What is JavaScript? Syntax and the Document Object Model Moving forward with JavaScript

Ajax Design and Usability

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Client-side Web Engineering From HTML to AJAX

A Tale of the Weaknesses of Current Client-side XSS Filtering

Developing ASP.NET MVC 4 Web Applications Course 20486A; 5 Days, Instructor-led

Short notes on webpage programming languages

Intrusion detection for web applications

ONLINE SCHEDULING FOR THE PRIVATE CLINIC "OUR DOCTOR" BASED ON WEB 2.0 TECHNOLOGIES

Mobile Web Applications. Gary Dubuque IT Research Architect Department of Revenue

The purpose of jquery is to make it much easier to use JavaScript on your website.

Web Development 1 A4 Project Description Web Architecture

Visualizing an OrientDB Graph Database with KeyLines

Web Application Guidelines

Pay with Amazon Integration Guide

Document Structure Integrity: A Robust Basis for Cross-Site Scripting Defense

Designing and Implementing an Online Bookstore Website

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

A send-a-friend application with ASP Smart Mailer

Working with forms in PHP

Web Application Exploits

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Web Application Security Considerations

Friday, February 11, Bruce

Accessing Data with ADOBE FLEX 4.6

Dynamic Web-Enabled Data Collection

Blufusion.net. Easy AJAX Post with jquery and CodeIgniter. Home. php javascript jquery codeigniter web development.

IMRG Peermap API Documentation V 5.0

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

Developing ASP.NET MVC 4 Web Applications MOC 20486

Spectrum Technology Platform

Visualizing a Neo4j Graph Database with KeyLines

Performance Testing for Ajax Applications

CMP3002 Advanced Web Technology

A Sane Approach to Modern Web Application Development. Adam Chlipala February 22, 2010

CSCI110 Exercise 4: Database - MySQL

InPost UK Limited GeoWidget Integration Guide Version 1.1

Webapps Vulnerability Report

Cross Site Scripting (XSS) and PHP Security. Anthony Ferrara NYPHP and OWASP Security Series June 30, 2011

Complete Cross-site Scripting Walkthrough

Introduction to Server-Side Programming. Charles Liu

AJAX and JSON Lessons Learned. Jim Riecken, Senior Software Engineer, Blackboard Inc.

Rich User Interfaces for Web-Based Corporate Applications

Table of contents. HTML5 Data Bindings SEO DMXzone

Intell-a-Keeper Reporting System Technical Programming Guide. Tracking your Bookings without going Nuts!

Web Security CS th November , Jonathan Francis Roscoe, Department of Computer Science, Aberystwyth University

Web Programming with PHP 5. The right tool for the right job.

Programming in HTML5 with JavaScript and CSS3

CSC309 Winter 2016 Lecture 3. Larry Zhang

A Tale of the Weaknesses of Current Client-Side XSS Filtering

jquery Programming Cookbook jquery Programming Cookbook

Portals and Hosted Files

Using an external style sheet with Dreamweaver (CS6)

Design Pa*erns for JavaScript Web Apps. Stefan Scheidt OPITZ CONSULTING GmbH

Advantage of Jquery: T his file is downloaded from

Developing ASP.NET MVC 4 Web Applications

WordPress Guide. v WorldTicket WordPress manual, 16 MAY / 23

HTML Basics(w3schools.com, 2013)

Advanced Web Development SCOPE OF WEB DEVELOPMENT INDUSTRY

Web Hosting Prep Lab Homework #2 This week: Setup free web hosting for your project Pick domain name and check whether it is available Lots of free

How to Create a Dynamic Webpage

Building Rich Internet Applications with PHP and Zend Framework

Understanding Cross Site Scripting

WEB APPLICATION SECURITY USING JSFLOW

Software Requirements Specification. Web Library Management System

A Server and Browser-Transparent CSRF Defense for Web 2.0 Applications. Slides by Connor Schnaith

Tiers of Web Programming - case of weborigami - Tetsuo Ida Department of Computer Science University of Tsukuba Japan

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Web Application Security

WIRIS quizzes web services Getting started with PHP and Java

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Tutorial básico del método AJAX con PHP y MySQL

The Feed URL is linked on your Module List under the Actions heading.

Cross-Platform Tools

Lecture 9 Chrome Extensions

What about MongoDB? can req.body.input 0; var date = new Date(); do {curdate = new Date();} while(curdate-date<10000)

Dimension Technology Solutions Team 2

Transcription:

AJAX with jquery ACM Webmonkeys 2011

What is AJAX? AJAX is a technology used to facilitate real-time data changes and updates on a page without requiring a page reload. AJAX stands for Asynchronous Javascript And XML. Let's break that down: Asynchronous: The response from the server doesn't have to be immediate, like a page load does. Other stuff can happen in-between. Javascript: The client-side language which you use to make requests to and handle responses from the server XML: The format often used to pass data between Javascript and the server.

No, really, what is AJAX? AJAX is basically a general term for making your webpage able to do dynamic stuff on the server (like make a new post, remove a user, etc) without having the user click on a link which loads a new page. It works similar to how any fancy JavaScript effect works, except that the JavaScript makes a call to some page on the server in the middle which causes something "real" to happen. Remember, JavaScript is client-side. It can't affect the database directly. It needs to use a technique like AJAX to cause an actual effect on the server.

A simple example get_stuff.php <?php echo "This is the stuff that will go in mypage.html the box.";?> <h1>stuff</h1> <div id="box"> </div> <a href='javascript:getstuff();'>get stuff via AJAX.</a> <script type='text/javascript'> function getstuff() { // Pseudocode for simplicity request = makehttprequest("get_stuff.php"); document.box.innerhtml = request.text; } </script>

What happens?

Main idea With AJAX, we use JavaScript to load a page, but the output is not loaded directly onto the page. The AJAX request will look the same to the server as if the user loaded the page in their browser. The output must be handled in JS to do what you want. Don't confuse normal JavaScript functionality with AJAX -- it only becomes AJAX when a remote call is involved. Kind of like RPCs for HTML.

So, how do we write pages with AJAX? There are two sides to an AJAX feature: The JavaScript, which makes the request The dynamic page, which does something and returns a response The dynamic side can be done in PHP, ASP, Ruby on Rails, C++, or anything that runs on your webserver. The JavaScript side needs to be done in JavaScript, of course, but we have a couple options: Straight, platform-specific JS (painful) Wrapper libraries (like Prototype or jquery)

What is jquery? http://www.jquery.com/ jquery is a JavaScript library based around the idea of attaching functionality to selected groups of elements. The main function in jquery is the selector, $, which works like: $("#thisisanid") -- finds the element with that id $("div") -- finds all divs Many more syntaxes are supported. Once a group of elements (or a single element) has been selected, you can call jquery functions that act on the entire group. It also has some very easy-to-use AJAX wrappers.

More on jquery The general structure of writing jquery is to define events on elements; for example: $("#somelink").click(function() { /*...*/ }); You typically pass in a function to the event function, which specifies what happens when that event is triggered. The function passed in is a closure, meaning it can refer to any variable in the current scope. $("somelink").ready(function() { $("somelink").fadeout(); }); // EVENT // ACTION

A simple example of jquery <script type="text/javascript" src="jquery.js"></script> <body> <div id="thediv" style="display:none;"> Content not yet loaded. </div> <script type="text/javascript"> $(document).ready(function() { $("#thediv").fadein(); }); </script> </body>

Adding AJAX That example doesn't use AJAX, just normal JavaScript. Let's add some AJAX by making the DIV load its contents from another page. For now, let's just use a static page. It makes no difference to JavaScript whether the response comes from a truly dynamic page, so using static pages is useful for testing your AJAX code. Let's first take a look at what jquery has to offer for AJAX...

jquery's shorthand AJAX methods Let's look more in depth at the get() method.

jquery.get() The get() function is a method of the jquery object, so we might call it like $.get('blah.php'). The full arguments are:

Making a GET request with AJAX As a reminder, GET data is passed as URL arguments (e. g.,?foo=bar&a=c&b=2). So, a GET request is just loading a specific URL with those arguments holding the request data. So, if we want to load the URL data.txt (no arguments being passed) and put the response (just the file's text) into the div, we could do: $.get('data.html', function(response) { alert("data incoming..."); $("#thediv").html(response); });

Even simpler If all you need to do is make a simple GET request, you can just use the load function (applied to an element directly). For example, we could just do: $("#thediv").load('data.html');

Applying AJAX Now that we've seen how to make an AJAX request, let's take a look at how to use it to improve our web pages. Remember, AJAX is just a way to make a web request without reloading the page. We can't do anything with it that we couldn't do without it, but we can make things seem faster, and not require reloading the page.

Checking username availability Suppose we have a site that lets users create accounts with custom usernames. However, no two users can share the same username, so during the registration process we need to let them know if there's a conflict. We could do this by checking after they submit the form, but let's write a system that automatically checks availability. The process we want is: User tabs out of the username field Make an AJAX request to see if username is taken Update form to display whether or not username is taken

The easy part -- server side We'll need a server-side script to check whether or not a username is taken. Let's write a username_check.php page which takes one argument, username, that we'll call like: username_check.php?username=blah It'll look something like: <?php // connect to database somehow... $sql = "SELECT 1 FROM users WHERE username='". mysql_real_escape_string($_get['username'])."' LIMIT 1"; $qry = mysql_query($sql); if (mysql_num_rows($qry) > 0) { echo "TAKEN"; } else { echo "AVAILABLE"; }?>

Adding an event to the text field Back on the client side, we need to add an event for when the user is done typing in their username. This event should be called when the field loses focus, so we use the. focusout() event handler in jquery. So, our code might be like: <input id='username' type='text' name='username' /> <div id='username_error_box'></div> <script type='text/javascript'> $("#username").focusout(function() { // ajax call will go here }); </script>

Handling the AJAX request We know how to make an AJAX request, so let's do it: $.get( 'username_check.php', { username: $("#username").val() }, function(response) { if (response == "TAKEN") { $('#username_error_box').html("this username is taken."); } });

Security concerns You might have noticed that an AJAX request just loads a page on your website. That is, anyone can load the same page, and supply their own arguments to make their own request. Even if you could enforce only requests coming from your page, it's easy for malicious users to modify the JavaScript on your page. So, you shouldn't pass sensitive information via AJAX, unless you take proper precautions in encrypting it (and remember, all encryption has to be done server-side; you can't do it via JavaScript). Also, if you're using a login system, make sure to check the session for proper credentials before fulfilling an AJAX request. This is a common attack vector.

Summary AJAX is a technique that lets you make your pages seem more responsive and flashier. If you use AJAX to perform sensitive tasks (like deleting data), make sure your server-side pages can't be exploited.