Giter Club home page Giter Club logo

ajax's Introduction

Ajax

Asynchronous - request are set without waiting JavaScript And XML - EXtensible Markup Language.

  1. Create an XMLHTTP Request object.
  2. Create a callback function.
  3. Open a request.
  4. Send the request.

Setting up Server: Php server

MAMP - Package Mac, Apache, MySQL and PHP

Apache is a web server software that is commonly used to run PHP. MySQL is a database engine.

https://www.mamp.info/en/downloads/

- Double click on it to unzip MAMP installer
- Double click to launch the MAMP installler
- click through the first 2 steps
- Continue (licence)
- install this for all users
- Browse Application folder to find MAMP folder and launch MAMP.
- click on preferences - ports - change the port on Apache from 8888 to 80.
- click start server.
- enter password

Ajax example

<script>
    var xhr = new XMLHttpRequest();
    // onreadystatechange is an event
    // and function after the event is a callback function.
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        document.getElementById('ajax').innerHTML = xhr.responseText;
      }
    };
    xhr.open('GET', 'sidebar.html');
    function sendAJAX() {
      xhr.send();
      document.getElementById("load").style.display = "none";
    }
</script>

Keywords: JSON - Javascript Object Notation JSON p - jquery provides easy way to work with jsonp XML

Ajax Limitations

Ajax is limited by the same origin policy, which controls how javascript control and access content from the webserver.

the ways around the same origin policy are

1. create Web Proxy - web services are not limited to the same origin policy. Servers can request data from servers from other domains.

2. JSON P Jason with Padding

it relies on the ability to link the Javascript file across domains.

Jquery is a javascript library

3. CORS - Another method to make ajax request across domains is CORS or Cross Origin Resource Sharing. It is W3 recommendation.

4. It has to be viewed through web servers.

JSON formatting:

JSON - There are two different ways to format JSON.

  1. An Array notation.
  2. An Object notation. and It is common to combine both.

Properties must be quoted using double quotes

Array example

['string',3,true,false,[1,2,3]]


Object
{
  "name": "Jim",
  "phone": "123-432-543"
}

Parsing JSON Data

with ajax the browser sends out the request to the webserver. and webserver sends response.

The response is just plain text and we need to take the string and convert it to Javascript.

Office Status Widget

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4){
    console.log(typeof xhr.responseText);
  }
};

xhr.open('GET', 'data/employees.json');
xhr.send();

The typeof method of XMLHttpRequest returns the following in the console.

=> XHR finished loading: GET "http://localhost/Ajax/data/employees.json".
String

The ResponseText return the String

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4){}
    console.log(xhr.responseText);
};

xhr.open('GET', 'data/employees.json');
xhr.send();

XHR finished loading: GET "http://localhost/Ajax/data/employees.json".(anonymous function) @ widget.js:19
widget.js:15 [
  {
   "name": "Aimee",
   "inoffice": false
  },
  {
   "name": "Amit",
   "inoffice": false
  },
  {
   "name": "Andrew",
   "inoffice": true
  },
  {
   "name": "Ben",
   "inoffice": true
  },
  {
   "name": "Elizabeth",
   "inoffice": true
  },
  {
   "name": "Guil",
   "inoffice": false
  },
  {
   "name": "Hampton",
   "inoffice": true
  },
  {
   "name": "Jason",
   "inoffice": true
  },
  {
   "name": "Lainie",
   "inoffice": true
  },
  {
   "name": "Kenneth",
   "inoffice": false
  },
  {
   "name": "Pasan",
   "inoffice": true
  },
  {
   "name": "Shawna",
   "inoffice": true
  },
  {
   "name": "Zac",
   "inoffice": false
  }
]


var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if(xhr.readyState === 4){
    //converting string to javascript object.
    var employees = JSON.parse(xhr.responseText);
    console.log(employees);
  }
};
xhr.open('GET', 'data/employees.json');
xhr.send();

this returns in console.

XHR finished loading: GET "http://localhost/Ajax/data/employees.json".(anonymous function) @ widget.js:21

widget.js:16 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

Processing JSON Data:

  1. Create new HTML list Item
  2. Check the "inoffice" property
  3. Get the value for the "name" property; Insert the inside the
  4. tag.
  5. close the
  6. tag
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if(xhr.readyState === 4) {
    var employees = JSON.parse(xhr.responseText);
    var statusHTML = '<ul class="bulleted">'; // open ul tag
    for (var i=0; i<employees.length; i+=1){
      if(employees[i].inoffice === true){
        statusHTML += '<li class="in">';
      } else {
        statusHTML += '<li class="out">';
      }
      statusHTML += employees[i].name;
      statusHTML += '</li>';
    }
    statusHTML += '</ul>';
    document.getElementById('employeeList').innerHTML = statusHTML;
  }
};
xhr.open('GET', 'data/employees.json');
xhr.send();

office widge in out

ajax's People

Contributors

rizali avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.