This repository was archived by the owner on Mar 21, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ const year = < HTMLElement > document . getElementById ( 'year' ) ; //get div you want to put year in
2+ year . innerHTML = new Date ( ) . getFullYear ( ) . toString ( ) ; //make html of div be the current year
Original file line number Diff line number Diff line change 1+ //function AJAX request
2+ function AjaxRequest ( search : string | null | undefined , uri : string ) { //in server api, search can be empty
3+
4+ //typical ajax call, used this guide to change from jquery: http://youmightnotneedjquery.com/#request
5+ const request = new XMLHttpRequest ( ) ;
6+ request . open ( 'POST' , uri , true ) ; //post request
7+ request . onload = function ( ) {
8+ if ( this . status >= 200 && this . status < 400 ) { //on success
9+ var resp = this . response ;
10+ console . log ( resp ) ;
11+ } else { //on fail
12+ console . log ( 'Error on the server. Check you have the correct url.' ) ;
13+ }
14+ } ;
15+ request . onerror = function ( ) { //other errors
16+ console . log ( 'Not able to connect to server' ) ;
17+ } ;
18+ request . send ( search ) ;
19+ }
20+
21+ const search_form = < HTMLElement > document . getElementById ( 'search-form' ) ; //get search form by its id
22+ search_form . addEventListener ( 'submit' , function ( event ) { //when form submitted
23+ event . preventDefault ( ) ;
24+ let search : string ;
25+ search = ( document . getElementById ( "search" ) as HTMLInputElement ) . value ; //get search text field value
26+ AjaxRequest ( search , '/api/search/' + search ) ; //send request to API function
27+ } )
You can’t perform that action at this time.
0 commit comments