0
所以我一直在这个工作几个小时,我很烂。所以起初我遇到了一个Cors问题,并解决了它。然后我尝试了几件事情。我想要做的就是让$resource
而不进行自定义发布方法。我的API是使用安宁的标准建立的。 POST /artist
接受具有两个属性的json。名称和密码。如何使用Angular将数据保存到我的快递服务器?
所以我尝试了一些东西,因为我一直在看着解决方案的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 **你是否通过这个?也更新了每次尝试的错误信息。 – Aravind
backendUrl只是'http:// localhost:1337 ' – jemiloii
它在代码中。更新错误到 – Aravind