Alvaro Molina Alvaro Molina - 4 months ago 15
Node.js Question

AngularJS don't send data to NodeJS server

Im new with NodeJS and Im trying to send data to server with Angular

$scope.logearse = function () {
method: "POST",
url: "http://localhost:8888/login",
data: $scope.usuario
}).then(function successCallback(response){
}, function errorCallback(error){
alert("No se han podido enviar los datos")

But in the server when I try to receipt the request is always

http.createServer(function(peticion, respuesta){

// We begin with "login"

if (peticion.url == "/login") {
console.log("Inside of Login)

var datosUsuarioLogin = '';

peticion.addListener("data", function(chunk) {
datosUsuarioLogin += chunk;

// function called when a new chunk of data is recibed

peticion.addListener("end", function() {
// When the data is recibed is transformed in JSON
var datosUsuarioLoginObjeto = querystring.parse(datosUsuarioLogin);
recuperarDatos(datosUsuarioLoginObjeto, respuesta);
console.log(datosUsuarioLoginObjeto) //return {}
//End of LOGIN "if"

The thing is the same code works if I use a form with a regular submit but no if I try to use the $http of ANGULAR.

I try to use "params" instead of "data" but "params" transform the data in the URL and the code dont works.


You need to use bodyParser to parse the request body and place the result in request.body of route.


And the request:

      method: "POST",
      url: "http://localhost:8888/login",
      contentType: "application/json",
      data: $scope.usuario // if usuario is not a valid json, you could to use JSON.stringify($scope.usuario);