gpt4 book ai didi

javascript - $http.post 奇怪的行为

转载 作者:行者123 更新时间:2023-11-28 04:58:37 29 4
gpt4 key购买 nike

我有一个组件,我曾经将其发布到 Rest API引擎。该组件看起来像:

(function(angular) {
'use strict';
angular.module('ComponentRelease', ['ServiceRelease'])
.component('createRelease', {
templateUrl: 'components/release/createRelease.html',
controller: CreateRelease,
controllerAs: 'clCtrl',
})

function CreateRelease($http, getReleaseManagers, insertRelease) {
var ctrl = this;

this.$onInit = function() {
getReleaseManagers.promise($http).then(function(response) {
ctrl.managers = response.data.releasemanager;
});

//the save button
ctrl.save = function() {
var release = { "rName": ctrl.r_name, "releaseDate": ctrl.r_date, "releaseSharepoint": ctrl.r_sharepoint, "gManager": ctrl.gname, "pManager": ctrl.pname };

$http.post('http://localhost:8080/post', release).then(function(response) {
console.log("Save in database");
}, function(e) {
console.log(e);

});
};
};
};
})(window.angular);

如果在控制台日志中保存正常,我会看到未定义,如果我有错误则显示错误消息。也许我没理解正确,但如果帖子没问题,我应该看到消息“保存在数据库中”。请您帮我理解为什么会出现这种情况?我正在使用angularjs 1.6

预先感谢您的帮助,欧根

最佳答案

在您的 Angular 帖子中,您还应该在配置中发送 header 内容类型,以便服务器正确解析数据类型(application/json)。

(function(angular) {
'use strict';
angular.module('ComponentRelease', ['ServiceRelease'])
.component('createRelease', {
templateUrl: 'components/release/createRelease.html',
controller: CreateRelease,
controllerAs: 'clCtrl',
})

function CreateRelease($http, getReleaseManagers, insertRelease) {
var ctrl = this;

this.$onInit = function() {
getReleaseManagers.promise($http).then(function(response) {
ctrl.managers = response.data.releasemanager;
});

//the save button
ctrl.save = function() {
var release = {
"rName": ctrl.r_name,
"releaseDate": ctrl.r_date,
"releaseSharepoint": ctrl.r_sharepoint,
"gManager": ctrl.gname,
"pManager": ctrl.pname
};

var config = {
headers: {
'Content-Type': 'application/json'
}
}

$http.post('http://localhost:8080/post', release, config).then(function(response) {
console.log("Save in database");
}, function(e) {
console.log(e);

});
};
};
};
})(window.angular);

关于javascript - $http.post 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42341466/

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