2016-02-25 70 views
1

我有形式,在json中使用角度輸出數據沒有數據庫。現在,無論是哪種形式的輸出,我都希望將它發送給某個服務器。下面是代碼,例如我想將它發送到服務器192.80.36.4。如何做到這一點使用後想要發送表單json數據到其他服務器

controller definition code 


<body ng-app="submitExample"> 
    <script> 
    angular.module('submitExample', []) 
    .controller('ExampleController', ['$scope', function($scope) { 
    $scope.list = []; 
    $scope.text = ''; 
    $scope.submit = function() { 
    if ($scope.text) { 
     $scope.list.push(this.text); 
     $scope.text = ''; 
    } 
    }; 
    }]); 
    </script> 

    </script> 
    <form ng-submit="submit()" ng-controller="ExampleController"> 
     Enter latitude: 
     <input type="text" ng-model="text" name="text" /> 
     <input type="submit" id="submit" value="Submit" /> 
     <pre>list={{list| json}}</pre> 
    </form> 

回答

3

您需要使用$http服務:

angular.module('submitExample', []) 
    .controller('ExampleController', ['$scope', '$http', function($scope, $http) { 
    $scope.list = []; 
    $scope.text = ''; 
    $scope.submit = function() { 
     if ($scope.text) { 
     $scope.list.push(this.text); 
     $scope.text = ''; 
     $http.post("<your-url>", $scope.list).success(function(data, status) { 
      console.log(data); 
     }) 
     } 
    }; 
    }]); 
+0

,我想將數據發送到服務器的網址是什麼? – koku19

+0

我不知道你在哪裏發送你的數據。您需要一臺能夠捕獲您的POST請求的服務器。 –

+0

XMLHttpRequest無法加載http://183.000.00.00/t/db/。對預檢請求的響應不會通過訪問控制檢查:請求的資源上不存在「訪問控制 - 允許來源」標頭。 Origin'example.com'因此不被允許訪問。 這是錯誤 – koku19

相關問題