I am trying to send a post request from my angularJS application which is running on 4200 port as follows to the nodeJS application running on different port 3000.
AngularJS 2.0 Code:
addNewItem(body:string) { const headers = new Headers(); headers.append('Content-Type','application/json'); return this.http.post('http://10.244.1.59:3000/newBinDevice',body,{ headers: headers }) .map((response : Response) => response.json()); } NodeJS code
var express = require('express'); module.exports = function(app){ var db = require('./dbclient'); var bodyParser = require('body-parser'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended:true})); db.dbconnection(null,null,null,null,'smartbin',null); app.post('/newItem', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); //res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Content-Type"); res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); console.log('body request is ' + req.body + " after stringifying it " + JSON.stringify(req.body) + " array to string " + req.body.toString()); } I am getting CORS issue saying that " Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 404." I read somewhere that one cannot send data other than
application/x-www-form-urlencoded multipart/form-data text/plain When I tried to send data as a string instead of JSON object from AngularJS application, i am getting empty data on nodejs application.