【问题标题】:How do I save data to my express server with Angular?如何使用 Angular 将数据保存到我的快递服务器?
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 2020-08-30
    • 2020-06-25
    • 1970-01-01
    • 2018-04-03
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多