2015-10-14 19 views
0

我如何將正常的RestApi改變成angularjs中的http post請求?如何我可以改變正常的RestApi成angularjs中的http post請求

我想發送這個url作爲$ http - 在angularjs函數中的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 
[email protected]://localhost:3000/app/components/assetmanagement/controllers/library.FolderCreation.js:76:6 
anonymous/[email protected]://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</[email protected]://localhost:3000/bower_components/angular/angular.js:15922:16 

但是,這是行不通的。任何想法請?

+0

已經給任何思想實際...做了谷歌搜索? – sirrocco

+0

@sirrocco:我複製了我的代碼,這是我試過的.. – User123

+0

@ User123 - 你在控制檯中得到的錯誤是什麼? –

回答

0

嘗試以下示例。

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; 
 
     } 
 
    ); 
 
    
 
    } 
 
    
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    <button type="button" ng-click="makeApiPostCall()">Make POST call</button> 
 
    <div ng-bind="postCallResponse"></div> 
 
</div>

+0

它只發送http:// localhost:3001/ams/v2 /文件夾,它不發送http:// localhost:3001/ams/v2 /文件夾?name = SampleCCCC – User123

+0

我剛纔編輯我的答案,使用查詢參數使用您的整個網址。 – vinagreti