【发布时间】:2016-12-03 04:42:45
【问题描述】:
所以我已经为此工作了几个小时,但我很糟糕。所以起初我遇到了一个cors问题,并解决了它。然后我尝试了几件事。我想要做的是拥有$resource 而不使用自定义发布方法。我的 api 是使用 restful 标准设置的。 POST /artist 接受具有两个属性的 json。名称和密码。
所以我尝试了一些方法,因为我一直在 stackoverflow 上寻找解决方案。这是我尝试过的,他们最终都没有向我的服务器发布任何数据。数据由 ng-model 设置,我可以通过 $scope.artist 查看数据。
尝试 1
$scope.submit = function () {
console.log('artist:', $scope.artist);
ArtistSignup.save($scope.artist).$promise
.then(function (res) {
console.log('res:', res);
})
.catch(function (error) {
console.log('error:', error.data.message);
});
};
尝试 2
$scope.submit = function () {
console.log('artist:', $scope.artist);
$scope.newArtist = new ArtistSignup();
$scope.newArtist.data = $scope.artist;
$scope.newArtist.$save($scope.artist)
.then(function (res) {
console.log('res:', res);
})
.catch(function (error) {
console.log('error:', error.data.message);
});
};
尝试 3
$scope.artist = new ArtistSignup();
$scope.submit = function () {
console.log('artist:', $scope.artist);
$scope.artist.$save()
.then(function (res) {
console.log('res:', res);
})
.catch(function (error) {
console.log('error:', error.data.message);
});
};
我的工厂,我试过有没有{name: '@name')
(function(){
'use strict';
angular.module('artistSignup')
.factory('ArtistSignup', function (backendUrl, $resource) {
return $resource(backendUrl + '/artist/:name', {name: '@name'});
});
}());
【问题讨论】:
-
backendUrl 你在传递这个吗?还要在每次尝试中用您的错误更新帖子。
-
backendUrl 只是
http://localhost:1337 -
它在代码中的什么位置。将错误更新到帖子
-
app.module(....., [...]).value('backendUrl', 'http://localhost:1337')
标签: javascript angularjs node.js express angular-resource