AJAX Basics and JSON Examples

ajax basics n.w
1 / 4
Embed
Share

This content covers the basics of AJAX, XMLHttpRequest, handling responses, JSON examples, and JavaScript code for parsing JSON data. It also includes a JSON output example, AJAX responses, and controller code for handling JSON data in web development.

  • AJAX Basics
  • JSON Examples
  • XMLHttpRequest
  • JavaScript
  • Web Development

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

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; } State 4 means done Raw text of response (also available as XML) CS 142 Lecture Notes: Ajax Slide 1

  2. JSON {name: "Alice", gpa: 3.5, friends: ["Bill", "Carol", "David"]} CS 142 Lecture Notes: Ajax Slide 2

  3. JSON Example Controller code: Class StudentsController < ApplicationController def get_students @students = Student.all render json: @students end end [{"advisor_id":"2","birth":"1987-10-22", "gpa":3.9,"grad":2009,"id":1, "name":"Anderson"}, {"advisor_id":"1","birth":"1990-04-16", "gpa":3.1,"grad":2012,"id":2, "name":"Jones"}, ... ] JSON Output Javascript in browser: var students = JSON.parse(xhr.responseText); element.innerHTML = students[1].name; AJAX response (from XMLHttpRequest object) Slide 3

  4. CS 142 Lecture Notes: Cookies Slide 4

More Related Content