1

發佈請求url應該按照以下格式進行更新。通過工廠在Angular 1.x中發送發佈請求

https://www.example.com/api/one-push?type=json&query=push&title=____&url=____&tag=___

<form ng-submit="submitUrl()"> 
<input type="text" class="form-control block" ng-model="url.title" placeholder="title"> 
    <input type="text" class="form-control block" ng-model="url.urlString" placeholder="url"> 
    <input type="text" class="form-control block" ng-model="url.tag" placeholder="tag"> 
    <button>Add</button> 
</form> 

var app = angular.module('app', []) 
.controller('searchController', ['$scope', '$http','searchService', function($scope, $http,searchService) { 
$scope.submitUrl = function() { 
    $scope.url = {}; 
     searchService.updateUrl($scope.url).success(function(data) { 
     $scope.url = data; 
     }) 
    } 
    }]); 

app.factory('searchService',function($http) { 
    var url = " https://www.example.com/api/one-push?"; 
    var Info = {}; 
    Info.updateUrl = function(url) { 
    return $http.post(url, { 
      type: "json", 
      url: url.title, 
      urlString: url.urlString, 
      tag: url.tag 
    }); 
    } 
    return Info; 
}); 

回答

0

簽名$ http.post方法是post(url, data, [config])

下面的配置是可選

當你要傳遞的數據作爲POST請求的查詢字符串,所以你必須設置params屬性在配置對象上。

廠:

app.factory('searchService',function($http) { 
    var url = " https://www.example.com/api/one-push"; 
    var Info = {}; 
    Info.updateUrl = function(url, data) { 
     var _data = data || {}; 

     return $http.post(url, _data, { 
      responseType: "json", 

      // Pass the data you want to pass as query params on request 
      params: { 
       type: "json", 
       url: _data.urlString, 
       query: 'push', 
       title: _data.title, 
       tag: _data.tag 
      } 
     }); 
    } 
    return Info; 
}); 
+0

感謝您的回答。另一種修正替換URL以_data和它的工作PARAMS:{ 類型: 「JSON」, 網址:_data.urlString, 查詢: '推', 標題:_data.title, 標籤:_data.tag } – rebello

0

您可以使用 「PARAMS」 來實現這一目標如下。

app.factory('searchService',function($http) { 
    var url = " https://www.example.com/api/one-push?"; 
    var Info = {}; 
    Info.updateUrl = function(url) { 
    return $http.post(url, { 
      type: "json", 
      params: {'type':'json','query':'push','title':title,'url':url,'tag':tag} 
    }); 
    } 
    return Info; 
});