Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 尝试使用$resource发布时出错_Angularjs - Fatal编程技术网

Angularjs 尝试使用$resource发布时出错

Angularjs 尝试使用$resource发布时出错,angularjs,Angularjs,我正在使用以下函数调用entityService.postEntity函数: entityService.postEntity($scope.entityType, formData) .then(function (result) { $scope.grid.data.push(result); }, function (result) { alert("Error: " + result); }) var EntityResource

我正在使用以下函数调用entityService.postEntity函数:

   entityService.postEntity($scope.entityType, formData)
   .then(function (result) {
      $scope.grid.data.push(result);
   }, function (result) {
      alert("Error: " + result);
   })
   var EntityResource = $resource('/api/:entityType', {}, {
      postEntity: { url: '/api/:entityType/', method: 'POST' },
   });

   postEntity: function (entityType, entity) {
      var deferred = $q.defer();
      EntityResource.postEntity({ entityType: entityType }, entity,
         function (resp) {
               deferred.resolve(resp);
         }, function (resp) {
               deferred.reject(resp.data.exceptionMessage);
         });
         return deferred.promise;
   },
以下是我的实体资源和postEntity函数:

   entityService.postEntity($scope.entityType, formData)
   .then(function (result) {
      $scope.grid.data.push(result);
   }, function (result) {
      alert("Error: " + result);
   })
   var EntityResource = $resource('/api/:entityType', {}, {
      postEntity: { url: '/api/:entityType/', method: 'POST' },
   });

   postEntity: function (entityType, entity) {
      var deferred = $q.defer();
      EntityResource.postEntity({ entityType: entityType }, entity,
         function (resp) {
               deferred.resolve(resp);
         }, function (resp) {
               deferred.reject(resp.data.exceptionMessage);
         });
         return deferred.promise;
   },
以上对我来说很有用

现在我尝试用以下内容替换此内容:

 var entityResource = $resource('/api/' + $scope.entityType + '/');
 entityResource.save({}, formData)
    .success(function (data, status, headers, config) {
       var a = data;
    })
    .error(function (data, status, headers, config) {
       var b = data;
    });
这给了我一个错误:

Object #<b> has no method 'success'
Object#没有方法“success”

有人能给我一些建议,说明我可能做错了什么。

$resource返回一个$then。像这样使用它:

var myThing = $resource(...);
var myResult = myThing.save({}, formData);
myResult.$then(function(data){}, function(data){});
//             success           failure

谢谢我试过这个,但它似乎不去。然后。我在所有东西上都设置了断点,它完全跳过了它。在那之前用$a正确吗?我还应该使用.save((),formData),其中formData是我的对象吗?对不起,我给了你错误的信息。我在上面编辑了我的代码。现在应该可以了。您的save调用返回一个承诺,该承诺在解决时应通过$then块。