0

I'm doing an app with node.js for server side and angular for frontend part. I'm having a problem with a simple post method, the problem is when I send my json to server, it is taking it like a json key. I'm using body-parser, maybe the problem is with that? This is my code:

Angular:

var request = $http({
    method: "POST",
    url: 'http://localhost:3000/register',
    data: {
        "username": "filip"
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});

node.js

var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var app = express();
var config = require("./config");
var mongodb = require("mongodb")

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

app.post("/register", function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);
    console.log(req.body);
});

This is what i get on the server like a request:

{ '{"username":"filip"}': '' }

I'm just started to learn node.js and i'm stuck with this simple thing, anybody help?

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
fvukovic
  • 699
  • 1
  • 7
  • 14

1 Answers1

1

You are using application/x-www-form-urlencoded as content-type, which is another mime type than JSON. To send JSON you can use

$http.post('http://localhost:3000/register', { "username": "filip" })

Notice that I skipped specifying the content-type in the headers. application/json is the default content-type for POST if you don't specify something else.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
  • ty for response, yea i try that but then i cant send my request to server.. error : Failed to load http://localhost:3000/register: 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:8080' is therefore not allowed access. – fvukovic Jan 04 '18 at 09:18
  • I install plugin cors and now when i got an 404 error when i use post method, but if use put method it's working properly..Why the post method is not working? – fvukovic Jan 04 '18 at 09:39
  • 1
    @fvukovic You can try setting up CORS as described in [this](https://stackoverflow.com/a/21455819/8574934) answer. You have `'http://localhost:8080'` as allowed origin in your question, but you are sending to `'http://localhost:3000'`. Try changing the allowed origin to `*` instead. – Mika Sundland Jan 04 '18 at 11:08