gpt4 book ai didi

javascript - 来自 Angular 形式的 $http.post 不发送任何数据

转载 作者:行者123 更新时间:2023-12-03 08:16:36 25 4
gpt4 key购买 nike

我有一个正在发布/api/tradelink 的表单,但它不发送任何正文或数据。

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>

服务:

.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();
}
}])

服务器端持有 api 请求,req.body 或 req.params 中没有任何内容。两者都显示为空对象。

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'
})
});

最佳答案

检查 Angular 文档中的 $http.post

您正在调用 Auth.send($scope.tradelink),但您的 authFactory.send() 函数需要接受此 tradelink 值,然后用作 $http.post() 的数据参数

所以:

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

关于javascript - 来自 Angular 形式的 $http.post 不发送任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33913252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com