我如何将正常的RestApi更改为angularjs中的http post请求

时间:2015-10-14 16:42:20

标签: angularjs

如何在angularjs中将正常的RestApi更改为http post请求?

我想在函数中的angularjs中将此URL发送为$ http - post请求。

http://localhost:3001/ams/v2/folder?name=SampleCCCCCC

我的代码:

$http.post('http://localhost:3001/ams/v2/folder?name=SampleCCCCCC', data, config)
.success(function(data, status, headers, config) {
    console.log("Server request is sending");
})
.error(function(data, status, header, config) {
    console.log("Server request is NoT sending");
});

我收到错误:

Error: data is not defined
$scope.ok@http://localhost:3000/app/components/assetmanagement/controllers/library.FolderCreation.js:76:6
anonymous/fn@http://localhost:3000/bower_components/angular/angular.js line 13275 > Function:2:194
@http://localhost:3000/bower_components/angular-touch/angular-touch.js:478:9
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:3000/bower_components/angular/angular.js:15922:16

但是,它没有用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试下面这个例子。

edit
var $scope = {};
var apiUrl = 'http://localhost:3001/ams/v2/folder?name=SampleCCCCCC';
var callData = {
  
}

var myApp = angular.module('myApp',[]);

myApp.controller('myCtrl', ['$scope', '$http', function($scope, $http){
  
  $scope.postCallResponse = 'Post not done!';
  
  $scope.makeApiPostCall = function(){
    
    $http.post(apiUrl, callData)
    .then(
      function(res){ // SUCCESS
        $scope.postCallResponse = res;
      }
      ,function(res){ // ERROR
        $scope.postCallResponse = res;
      }
      ,function(res){ // FINALLY
        $scope.postCallResponse = res;
      }
    );
    
  }
  
}]);