angularjs - Sending a Post request in Angular 1.x via factory -
the post request url should updated in following format.
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; });
signature of $http.post method post(url, data, [config])
here config optional
as want pass data query string on post request, have set params
property on config object.
factory:
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 data want pass query params on request params: { type: "json", url: _data.urlstring, query: 'push', title: _data.title, tag: _data.tag } }); } return info; });
Comments
Post a Comment