About cross-domain issues with AJAX Access Express servers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.