简体   繁体   中英

$http.post from angular form not sending any data

I have a form that is posting /api/tradelink but it doesn't send any body or data with it.

HTML :

<form ng-submit="sendTradelink()">
            <md-input-container class="md-accent">
                <label>Enter your tradelink</label>
                <input ng-model="tradelink">
            </md-input-container>
            <md-button type="submit" class="md-raised md-accent">Send</md-button>
        </form>

Services :

.factory('Auth', ['$http', '$api', '$window', function ($http, $api, $window) {
        var authFactory = {};
        authFactory.authenticate = function(){
            $http.post($api.url + 'auth')
            .successs(function(url){
                $window.location.href = url;
            });
        };
        authFactory.send = function () {
            return $http.post($api.url + 'tradelink');
        };
        return authFactory;
    }]);

Controller ;

.controller('AppCtrl', ['$scope', 'Auth', '$location', '$cookies', function ($scope, Auth, $location, $cookies) {
        var sidTemp = 'needtomakeitanewvalue';
        $scope.checklogin = function () {
            $scope.sid = $cookies.get('sid2');
            console.log($scope.sid);
        }
        $scope.sendTradelink = function () {
            Auth.send($scope.tradelink)
                .success(function (res) {
                    $scope.sidTemp = 'needtomakeitanewvalue';
                    $cookies.put('sid2', sidTemp);
                    $location.path('/');
                });
        }
        $scope.auth = function () {
            Auth.authenticate();
        }
    }])

Server side holding api request, nothing inside req.body or req.params. Both show as empty objects.

api.post('/tradelink', function(req, res){
    console.log(req.user.steamId);
    console.log(req.params);
    console.log(req.body);
    res.json({
      success: true,
      message: 'tradelink received'
    })
  });

Check the Angular docs for $http.post

You are calling Auth.send($scope.tradelink), but your authFactory.send() function needs to accept this tradelink value and then be used as a data param to $http.post()

So:

authFactory.send = function (tradelink) {
            return $http.post($api.url + 'tradelink', {tradelinkId: tradelink });
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM