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; 
}); 
相关问题