Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en" id="html">
- <head>
- <meta charset="UTF-8">
- <title>XHR</title>
- <style>
- html,
- body {
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .container {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-content: center;
- align-items: center;
- margin-top: 30px;
- position: relative;
- }
- </style>
- </head>
- <body>
- <div id="container" class="container">
- </div>
- <script>
- 'use strict'
- {
- function processResponse(json)
- {
- let users = JSON.parse(json);
- if (users.length == 0) {
- console.log('No such user!');
- }
- for (let user of users) {
- console.log(`uid: ${user.id} name: ${user.name} nickname: ${user.username}`);
- }
- }
- let xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://jsonplaceholder.typicode.com/users?id=100');
- //xhr.addEventListener('readystatechange', function () {
- // //console.log(this.readyState);
- // if (this.readyState == 4) {
- // if (this.status >= 400) {
- // console.log(`XHR ERROR! ${this.status}`);
- // }
- // else {
- // let users = JSON.parse(this.response);
- // //console.log(users);
- // for (let user of users) {
- // console.log(`uid: ${user.id} name: ${user.name} nickname: ${user.username}`);
- // }
- // }
- // }
- //});
- xhr.addEventListener('load', function () {
- if (this.status >= 400) {
- console.log(`XHR ERROR! ${this.status}`);
- }
- else {
- processResponse(this.response);
- }
- });
- xhr.send();
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement