1

I try to learn Express/NodeJS, I work with c9 ide. To use passport framework for authentication, I try to implement a very simple login form, but when I try to send my login informations to my server, he receive the data like this :

{ '{"username":"login","password":"pwd"}': '' }

Instead of the data I log before send :

OBJECT {username: "login", password: "pwd"}

And JSLint said to me it's a valid JSON, why it's send as a key of json object's attribute?

This is my Express server :

var application_root = __dirname,
express = require("express"),
path = require("path"),
mongoose = require('mongoose');

var app = express();
mongoose.connect('mongodb://' + process.env.IP + '/database');
app.configure(function () {
    app.use(express.bodyParser());
    app.use(express.methodOverride());

    var allowCrossDomain = function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
        res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");

        if ('OPTIONS' == req.method) {
          res.send(200);
        } else {
          next();
        }
    };

    app.use(allowCrossDomain);
    app.use(app.router);
    app.use(express.static(path.join(application_root, "public")));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.post('/login', function(req, res, next){
    var obj = req.body;
    console.log(obj);
});

app.listen(process.env.PORT, process.env.IP);

This is my html form :

<div>
    Username :
    <input ng-model="user.username"/>
    Password :
    <input type="password" ng-model="user.password"/>
    <div class="button" ng-click="login()">Login</div>
</div>

This is my angularjs code :

var app = angular.module('app', []).config( function($locationProvider, $routeProvider, $httpProvider) {

$routeProvider
    .when('/', {
        templateUrl: '/partials/home.html',
        controller : 'HomeCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });

$locationProvider.html5Mode(true);
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.withCredentials = true;

}).controller('HomeCtrl', function($scope, $http) {

    $scope.login = function() {
        $http.post('http://website.io/login', $scope.user).success(function(data) {
            console.log(data);
        });
    }; 
})

It's probably a simple problem, but my brain freeze on this now... Sorry for my english and thanking you in anticipation.

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Electron
  • 85
  • 7
  • I'm not sure but I think it's : $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; but application/json send nothing to my server... – Electron May 23 '13 at 08:42

2 Answers2

0

try to post angular.toJson($scope.user);

i think it's because it's a wrapped angular object

0

I have the same problem but you might already solve this issue I just fixed it by use option TransformRequest

like this

var transform = function(data){
    return $.param(data);
 }

$http.post("/foo/bar", requestData, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    transformRequest: transform
}).success(function(responseData) {
    //do stuff with response
});

I found from this link

Community
  • 1
  • 1
Whai Kung
  • 56
  • 4