如何使用Angular将数据保存到我的快速服务器?

时间:2021-04-25 20:33:01

So I been working on this for a few hours and I am suck. So at first I had a cors issue, and solved it. Then I tried several things. What I want to do is to have $resource without making a custom post method. My api is set up using restful standards. POST /artist accepts json with two properties. name and password.

所以我一直在研究这几个小时,我很糟糕。所以起初我有一个cors问题,并解决了它。然后我尝试了几件事。我想要做的是拥有$ resource而不需要自定义post方法。我的api是使用restful标准设置的。 POST / artist接受带有两个属性的json。名称和密码。

So I tried a few things, as I been on * looking at solutions. Here is what I've tried and they all end up posting no data to my server. The data is being set by ng-model and I can see the data with $scope.artist.

所以我尝试了一些东西,因为我一直在使用*查看解决方案。这是我尝试过的,他们最终都没有向我的服务器发布任何数据。数据由ng-model设置,我可以使用$ scope.artist查看数据。

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

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

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

my factory, i've tried with and without {name: '@name')

我的工厂,我试过有没有{名称:'@ name')

(function(){
    'use strict';
    angular.module('artistSignup')
        .factory('ArtistSignup', function (backendUrl, $resource) {
            return $resource(backendUrl + '/artist/:name', {name: '@name'});
    });
}());

1 个解决方案

#1


0  

So I actually figured it out... It was the express sanitizer I was using. It was creating an undefined req.body :( Rip...

所以我真的想通了......这是我正在使用的快速消毒剂。它创建了一个未定义的req.body :( Rip ......

#1


0  

So I actually figured it out... It was the express sanitizer I was using. It was creating an undefined req.body :( Rip...

所以我真的想通了......这是我正在使用的快速消毒剂。它创建了一个未定义的req.body :( Rip ......