我想从网页调用restate服务传递post方法的json数据,因为我们可以在高级rest客户端中执行
答案 0 :(得分:0)
您可以通过创建如下服务来实现:
angular.module('myApp.myService', [])
.factory('MyService', [
'$http',
'$q',
function($http, $q) {
var service = {
getJsonData: function getJsonData() {
return $http.post("http://www.returnjsondata.com");
}
}
return service;
}
]);
然后在你的控制器中,使用这样的服务:
angular.module('myApp.myController', [
'myApp.myService'
])
.controller('MyController', [
'$scope',
'MyService',
function($scope, MyService) {
function callServiceToGetJsonData() {
MyService.getJsonData()
.success(function(response) {
// The response returned should be the json data you're looking for
// Use it however you like here.
})
.error(function(response) {
// Error case
console.log("Error calling the service!");
});
}
}
]);
此处有更多信息:https://docs.angularjs.org/api/ng/service/ $ http