When learning es6, I used promise to encapsulate an Ajax
<script type= "Text/javascript" >functionGetnews (URL) {Let promise=NewPromise ((resolve,reject) ={ //State Initialization //Performing asynchronous TasksLet xmlHttp=NewXMLHttpRequest ();//Creating ObjectsConsole.log (xmlhttp.readystate);//the initial state is 1 steps to 4 //Binding MonitoringXmlhttp.onreadystatechange = () ={ if(Xmlhttp.readystate = = 4) {//Request succeeded if(Xmlhttp.status = = 200) {console.log (xmlhttp.responsetext); //Modify StatusResolve (Xmlhttp.responsetext); } Else{//request failedreject (' No news at the moment '); } } } //open Setting the way and URL of the requestXmlhttp.open (' GET ', URL); Xmlhttp.send (); }); returnpromise; } getnews (' http://localhost:3000 '). Then (data={console.log (data); },err={console.log (err); })</script>
With express write a simple server routing, Port 3000, but has been error, the original is not cross-domain, and then find a solution on the Internet, see the online add a lot of head, I only add the error of which one can be, as follows
Let Express = require (' Express '= Express (); App.get ('/', (req,res) + ={ Res.header (" Access-control-allow-origin "," * "); Res.send (' 123 ');}) App.listen (3000);
Finally on the browser side can see the return of 123
About cross-domain issues with AJAX Access Express servers