File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < body >
4+ < div id ="message "> </ div >
5+ < script >
6+
7+ const message = document . getElementById ( 'message' ) ;
8+
9+ const xhr = new XMLHttpRequest ( ) ;
10+ xhr . onreadystatechange = ( ) => {
11+ if ( xhr . readyState === XMLHttpRequest . DONE ) {
12+ message . innerHTML = ( xhr . status === 200 ?
13+ xhr . responseText : 'Error code: ' + xhr . status
14+ ) ;
15+ }
16+ } ;
17+ xhr . open ( 'GET' , '/person' , true ) ;
18+ xhr . send ( ) ;
19+
20+ </ script >
21+ </ body >
22+ </ html >
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const fs = require ( 'fs' ) ;
4+ const http = require ( 'http' ) ;
5+
6+ const index = fs . readFileSync ( './3-xhr.html' ) ;
7+
8+ http . createServer ( ( req , res ) => {
9+ if ( req . url === '/person' ) {
10+ res . end ( JSON . stringify ( { name : 'Marcus' } ) ) ;
11+ } else {
12+ res . end ( index ) ;
13+ }
14+ } ) . listen ( 8000 ) ;
You can’t perform that action at this time.
0 commit comments