Wednesday 17 May 2017

Uncaught (in promise): Response with status: 401 Unauthorized for URL: http://localhost:8080/api/contacts/

This is my service class where I have made the providers to create a contact with post method, which requires jwt Authozisation at the server side, when I post or trigger this endpoint in order to post the data through this it displays the error.

Part of my code which has to run.

Service.ts

createContacts(contact) {
    console.log('In service class the value of contact is', contact);
    return new Promise((resolve, reject) => {
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      headers.append('Authorization', this.auth.token);

      this.http.post('http://localhost:8080/api/contacts/', contact, { headers: headers }).map(res => res.json())
        .subscribe(res => {
          resolve(res);
        }, (err) => {
          reject(err);
        });
    });

  }

server.js

var authControllers = require('./controllers/authentication');
var contactControllers = require('./controllers/contact');
var express = require('express');
var passportService = require('./config/passport');
var passport = require('passport');

var requireAuth = passport.authenticate('jwt', { session: false });
module.exports = function (app) {
  var contactRoutes = express.Router();
 app.use('/api/contacts', contactRoutes);
  contactRoutes.post('/', requireAuth, function (req, res) {
        console.log("inside post routes of contacts");
        contactControllers.postContacts(req, res);
    });
}



via Aditya Jain

No comments:

Post a Comment