zone.js:2969 Access to XMLHttpRequest has been blocked by CORS policy





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using Angular to connect RestAPI. I got an error




zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.




But this is my POSTMAN result,
enter image description here



Here is my Nodejs code






const express    = require('express');
const app = express();
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8000;

var router = express.Router();

app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});

app.use('/', router);

router.get('/api', function(req, res, next) {
res.json({ message: 'hello' });
});

router.post('/auth/api/authen', function(req, res, next){

res.json({
success : true,
result : req.body
});

});

// START THE SERVER
app.listen(port);
console.log('Magic happens on port ' + port);








private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

login(username:string, password:string, ipaddress:string ) : Observable<any> {
return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
.pipe(
map(res => {
console.log(res);
return res;
})
);
}





Everything seems very simple but I cannot send a request from Angular app to the server. Please help.
Thanks.










share|improve this question





























    0















    I'm using Angular to connect RestAPI. I got an error




    zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.




    But this is my POSTMAN result,
    enter image description here



    Here is my Nodejs code






    const express    = require('express');
    const app = express();
    const bodyParser = require('body-parser');

    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());

    var port = process.env.PORT || 8000;

    var router = express.Router();

    app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
    });

    app.use('/', router);

    router.get('/api', function(req, res, next) {
    res.json({ message: 'hello' });
    });

    router.post('/auth/api/authen', function(req, res, next){

    res.json({
    success : true,
    result : req.body
    });

    });

    // START THE SERVER
    app.listen(port);
    console.log('Magic happens on port ' + port);








    private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

    login(username:string, password:string, ipaddress:string ) : Observable<any> {
    return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
    .pipe(
    map(res => {
    console.log(res);
    return res;
    })
    );
    }





    Everything seems very simple but I cannot send a request from Angular app to the server. Please help.
    Thanks.










    share|improve this question

























      0












      0








      0








      I'm using Angular to connect RestAPI. I got an error




      zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.




      But this is my POSTMAN result,
      enter image description here



      Here is my Nodejs code






      const express    = require('express');
      const app = express();
      const bodyParser = require('body-parser');

      app.use(bodyParser.urlencoded({ extended: true }));
      app.use(bodyParser.json());

      var port = process.env.PORT || 8000;

      var router = express.Router();

      app.all('*', function(req, res, next) {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
      });

      app.use('/', router);

      router.get('/api', function(req, res, next) {
      res.json({ message: 'hello' });
      });

      router.post('/auth/api/authen', function(req, res, next){

      res.json({
      success : true,
      result : req.body
      });

      });

      // START THE SERVER
      app.listen(port);
      console.log('Magic happens on port ' + port);








      private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

      login(username:string, password:string, ipaddress:string ) : Observable<any> {
      return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
      .pipe(
      map(res => {
      console.log(res);
      return res;
      })
      );
      }





      Everything seems very simple but I cannot send a request from Angular app to the server. Please help.
      Thanks.










      share|improve this question














      I'm using Angular to connect RestAPI. I got an error




      zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.




      But this is my POSTMAN result,
      enter image description here



      Here is my Nodejs code






      const express    = require('express');
      const app = express();
      const bodyParser = require('body-parser');

      app.use(bodyParser.urlencoded({ extended: true }));
      app.use(bodyParser.json());

      var port = process.env.PORT || 8000;

      var router = express.Router();

      app.all('*', function(req, res, next) {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
      });

      app.use('/', router);

      router.get('/api', function(req, res, next) {
      res.json({ message: 'hello' });
      });

      router.post('/auth/api/authen', function(req, res, next){

      res.json({
      success : true,
      result : req.body
      });

      });

      // START THE SERVER
      app.listen(port);
      console.log('Magic happens on port ' + port);








      private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

      login(username:string, password:string, ipaddress:string ) : Observable<any> {
      return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
      .pipe(
      map(res => {
      console.log(res);
      return res;
      })
      );
      }





      Everything seems very simple but I cannot send a request from Angular app to the server. Please help.
      Thanks.






      const express    = require('express');
      const app = express();
      const bodyParser = require('body-parser');

      app.use(bodyParser.urlencoded({ extended: true }));
      app.use(bodyParser.json());

      var port = process.env.PORT || 8000;

      var router = express.Router();

      app.all('*', function(req, res, next) {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
      });

      app.use('/', router);

      router.get('/api', function(req, res, next) {
      res.json({ message: 'hello' });
      });

      router.post('/auth/api/authen', function(req, res, next){

      res.json({
      success : true,
      result : req.body
      });

      });

      // START THE SERVER
      app.listen(port);
      console.log('Magic happens on port ' + port);





      const express    = require('express');
      const app = express();
      const bodyParser = require('body-parser');

      app.use(bodyParser.urlencoded({ extended: true }));
      app.use(bodyParser.json());

      var port = process.env.PORT || 8000;

      var router = express.Router();

      app.all('*', function(req, res, next) {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'DELETE, HEAD, GET, OPTIONS, POST, PUT');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
      });

      app.use('/', router);

      router.get('/api', function(req, res, next) {
      res.json({ message: 'hello' });
      });

      router.post('/auth/api/authen', function(req, res, next){

      res.json({
      success : true,
      result : req.body
      });

      });

      // START THE SERVER
      app.listen(port);
      console.log('Magic happens on port ' + port);





      private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

      login(username:string, password:string, ipaddress:string ) : Observable<any> {
      return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
      .pipe(
      map(res => {
      console.log(res);
      return res;
      })
      );
      }





      private options = { headers: new HttpHeaders().set('Content-Type', 'application/json') }

      login(username:string, password:string, ipaddress:string ) : Observable<any> {
      return this.http.post<any>( this.baseUrl + '/auth/api/authen', { username, password, ipaddress }, this.options )
      .pipe(
      map(res => {
      console.log(res);
      return res;
      })
      );
      }






      angular express






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 6:31









      Hikaru ShindoHikaru Shindo

      6023932




      6023932
























          3 Answers
          3






          active

          oldest

          votes


















          3














          The error tells that, for some reason, request has been sent as localhost:8000/auth/api/authen, which is not supported for protocol schemes:




          zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from >origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin >requests are only supported for protocol schemes: http, data, chrome, chrome->extension, https.




          If you change your request to http://localhost:8000/auth/api/authen, this error disappears.






          share|improve this answer































            0














            In Your Node application you need to enable CORS, As it is clear in the error Request is being blocked by CORS issue, So just these lines of code -



            var cors = require('cors')

            var app = express()
            app.use(cors());


            This will enable your HTTP request for any domain, you can customise origin list as per your requirements.






            share|improve this answer































              0














              Actually, both @Pardeep Jain & @vadi are right.



              You should try CORS first https://expressjs.com/en/resources/middleware/cors.html then change your URL request to back-end from localhost:3000/auth/api/authen to http://localhost:3000/auth/api/authen.



              Just in case your Options request has blocked, should be config it by the link above.






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348845%2fzone-js2969-access-to-xmlhttprequest-has-been-blocked-by-cors-policy%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                The error tells that, for some reason, request has been sent as localhost:8000/auth/api/authen, which is not supported for protocol schemes:




                zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from >origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin >requests are only supported for protocol schemes: http, data, chrome, chrome->extension, https.




                If you change your request to http://localhost:8000/auth/api/authen, this error disappears.






                share|improve this answer




























                  3














                  The error tells that, for some reason, request has been sent as localhost:8000/auth/api/authen, which is not supported for protocol schemes:




                  zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from >origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin >requests are only supported for protocol schemes: http, data, chrome, chrome->extension, https.




                  If you change your request to http://localhost:8000/auth/api/authen, this error disappears.






                  share|improve this answer


























                    3












                    3








                    3







                    The error tells that, for some reason, request has been sent as localhost:8000/auth/api/authen, which is not supported for protocol schemes:




                    zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from >origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin >requests are only supported for protocol schemes: http, data, chrome, chrome->extension, https.




                    If you change your request to http://localhost:8000/auth/api/authen, this error disappears.






                    share|improve this answer













                    The error tells that, for some reason, request has been sent as localhost:8000/auth/api/authen, which is not supported for protocol schemes:




                    zone.js:2969 Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from >origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin >requests are only supported for protocol schemes: http, data, chrome, chrome->extension, https.




                    If you change your request to http://localhost:8000/auth/api/authen, this error disappears.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 14 at 10:09









                    VadiVadi

                    1,229517




                    1,229517

























                        0














                        In Your Node application you need to enable CORS, As it is clear in the error Request is being blocked by CORS issue, So just these lines of code -



                        var cors = require('cors')

                        var app = express()
                        app.use(cors());


                        This will enable your HTTP request for any domain, you can customise origin list as per your requirements.






                        share|improve this answer




























                          0














                          In Your Node application you need to enable CORS, As it is clear in the error Request is being blocked by CORS issue, So just these lines of code -



                          var cors = require('cors')

                          var app = express()
                          app.use(cors());


                          This will enable your HTTP request for any domain, you can customise origin list as per your requirements.






                          share|improve this answer


























                            0












                            0








                            0







                            In Your Node application you need to enable CORS, As it is clear in the error Request is being blocked by CORS issue, So just these lines of code -



                            var cors = require('cors')

                            var app = express()
                            app.use(cors());


                            This will enable your HTTP request for any domain, you can customise origin list as per your requirements.






                            share|improve this answer













                            In Your Node application you need to enable CORS, As it is clear in the error Request is being blocked by CORS issue, So just these lines of code -



                            var cors = require('cors')

                            var app = express()
                            app.use(cors());


                            This will enable your HTTP request for any domain, you can customise origin list as per your requirements.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 17 '18 at 6:59









                            Pardeep JainPardeep Jain

                            43k17104147




                            43k17104147























                                0














                                Actually, both @Pardeep Jain & @vadi are right.



                                You should try CORS first https://expressjs.com/en/resources/middleware/cors.html then change your URL request to back-end from localhost:3000/auth/api/authen to http://localhost:3000/auth/api/authen.



                                Just in case your Options request has blocked, should be config it by the link above.






                                share|improve this answer




























                                  0














                                  Actually, both @Pardeep Jain & @vadi are right.



                                  You should try CORS first https://expressjs.com/en/resources/middleware/cors.html then change your URL request to back-end from localhost:3000/auth/api/authen to http://localhost:3000/auth/api/authen.



                                  Just in case your Options request has blocked, should be config it by the link above.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Actually, both @Pardeep Jain & @vadi are right.



                                    You should try CORS first https://expressjs.com/en/resources/middleware/cors.html then change your URL request to back-end from localhost:3000/auth/api/authen to http://localhost:3000/auth/api/authen.



                                    Just in case your Options request has blocked, should be config it by the link above.






                                    share|improve this answer













                                    Actually, both @Pardeep Jain & @vadi are right.



                                    You should try CORS first https://expressjs.com/en/resources/middleware/cors.html then change your URL request to back-end from localhost:3000/auth/api/authen to http://localhost:3000/auth/api/authen.



                                    Just in case your Options request has blocked, should be config it by the link above.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Apr 5 at 3:36









                                    namnhnamnh

                                    114




                                    114






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348845%2fzone-js2969-access-to-xmlhttprequest-has-been-blocked-by-cors-policy%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        List item for chat from Array inside array React Native

                                        Thiostrepton

                                        Caerphilly