1 / 4

Introduction to AJAX with XMLHttpRequest for Asynchronous Web Applications

This guide covers the basics of AJAX using the `XMLHttpRequest` object, focusing on how to initiate a POST request and handle responses effectively. We will explore the `onreadystatechange` event to manage the request's state and response. The section includes practical examples illustrating how to set up AJAX calls, handle errors, and process both text and XML responses. Additionally, we’ll look into higher-level AJAX interactions, particularly with JSON data, and how to dynamically update web elements upon user input.

harry
Télécharger la présentation

Introduction to AJAX with XMLHttpRequest for Asynchronous Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AJAX Basics xhr = new XMLHttpRequest(); xhr.onreadystatechange = xhrHandler(); xhr.open("POST", url); xhr.send(postData); ... function xhrHandler() { if (this.readyState != 4) { return; } if (this.status != 200) { // Handle error ... return; } ... var text = this.responseText; var document = this.responseXML; } State 4 means “done” Response available as raw text or XML CS 142 Lecture Notes: Forms

  2. Higher-Level AJAX Example Watch this element for change <%= observe_field( "userName", :frequency => 0.25, :update => "completionMenu", :url => {:action => "nameChoices"} ) %> Issue AJAX request here Replace this element’s innerHTML with response CS 142 Lecture Notes: Forms

  3. JSON Example {name: "Alice", gpa: 3.5, friends: ["Bill", "Carol", "David"]} CS 142 Lecture Notes: Forms

  4. CS 142 Lecture Notes: Forms

More Related