K
K
Kostya Ostapuk2018-02-13 21:12:57
Angular
Kostya Ostapuk, 2018-02-13 21:12:57

POST request error in Angular 5?

Failed to load https://github.com/login/oauth/access_token: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' 127.0.0.1:4200 ' is therefore not allowed access. The response had HTTP status code 404.
Trying to execute the following request

getToken(){
    this.route.queryParams.subscribe(params=>{
      this.githubCode=params['code'];
      localStorage.setItem('githubCode',this.githubCode);

      let headers = new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      headers.append("cache-control", "no-cache");
      let body={
        client_id: this.clientID,
        client_secret: this.clientSecret,
        code: localStorage.getItem('githubCode'),
        redirect_uri: this.redirectURI
      }
      let url='https://github.com/login/oauth/access_token';
      console.log(body);
      this._http.post(url,body, {headers})
        .subscribe(res => console.log(res.json()));
    });

  }

My server is running and the server code is:
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');
const http = require('http');
const app = express();

app.use(cors({
  origin: 'http://127.0.0.1:4200',
  credentials: true
}));


app.use(function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST');
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
   next();
});

// API file for interacting with MongoDB
const api = require('./src/server/api');

// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));

// API location
app.use('/api', api);

// Send all other requests to the Angular app
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'src/index.html'));
});

//Set Port
const port = process.env.PORT || '4200';
app.set('port', port);

const server = http.createServer(app);

server.listen(port, () => console.log(`Running on localhost:${port}`));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Epifanov, 2018-02-14
@kacheleff

The fact is that before sending a simple request, an OPTIONS request will first be sent. You must give the correct response to this request, otherwise the browser will block the server's response. An example can be seen here

D
dotajax, 2018-02-14
@dotajax

You send a request to https://github.com/login/oauth/access_token but it doesn't allow access because there are no headers like Access-Control-Allow. The error also contains a 404 page response code, indicating that the page was not found.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question