gpt4 book ai didi

AngularJS 与 ngResource : How to check exact response from server?

转载 作者:行者123 更新时间:2023-12-04 02:02:22 28 4
gpt4 key购买 nike

我创建了一个 angular 服务来与一个用 PHP 制作的简单 REST 服务器进行对话。我可以看到我能够获得单个记录、所有记录的列表并添加新记录,但是,在添加新记录后,我遇到了从服务器获取正确响应以便能够对其采取行动的问题。

我知道它正在工作,因为正在添加新记录,但是,如果由于某种原因请求不起作用,我希望向用户发出通知,等等......

这是服务:

angular.module('adminApp.services', ['ngResource'])

.factory('Settings', function($resource) {

return $resource('rest/setting/:id', {id: '@id'}, {
'query' : { method: 'GET', params: {}, format: 'json', isArray: true },
'save' : { method: 'POST', params: {}, format: 'json', isArray: true },
'get' : { method: 'GET', params: {}, format: 'json', isArray: false },
'update': { method: 'PUT', params: {id: '@id'}, format: 'json', isArray: true },
'delete': { method: 'DELETE', params: {id: '@id'}, format: 'json', isArray: false }
});

});

作为 Controller 的一部分,我有以下内容:
$scope.save = function() {
var result = Settings.save({}, $scope.settings);

console.log(result);

// Here I would like to send the result to the dialog
// to determine wether or not the request was successful
// dialog.close(result);
};

来自 HTTP 请求的网络响应,如通过 javascript 控制台所见,返回从服务器返回的“true”,但是,console.log(result) 返回“true”中的字符数组——我已经猜到了是因为 'save' 中的 isArray : true 选项,这是必要的,因为参数作为数组发送到服务器:
[$promise: Object, $resolved: false]
0: "t",
1: "r",
2: "u",
3: "e",
$promise: Object,

// I tried passing result.$resolved to the dialog,
// but if yousee above it resolves to false up top first
$resolved: true,
length: 4,
__proto__: Array[0]

我知道 HTTP 响应的 json 值是 true,如果我可以使用它会很容易(我来自 jQuery 背景,也许我做错了,我已经指出要从这里完全删除 jQuery项目以不允许它抑制我的学习)。

我想问题是,我如何真正从服务器获得响应到我可以实际使用的 JS 变量?

编辑:更新

我将我的服务更改为:
angular.module('adminApp.services', ['ngResource'])
.factory('Settings', function($http, $resource, $log) {
return $resource('rest/setting/:id', {id: '@id'}, {
save : {
method: 'POST',
params: {},
format: 'json',
isArray: true,
transformResponse: [function(data, headersGetter) {
$log.info(data); // returns true
return { response: data };
}].concat($http.defaults.transformResponse)
},
update : { method: 'PUT', params: {id: '@id'}, format: 'json', isArray: true },
delete : { method: 'DELETE', params: {id: '@id'}, format: 'json', isArray: false }
});

});

并呼吁:
$scope.save = function() {
$scope.results = Settings.save({}, $scope.settings);
console.log($scope.results); // Still returns the response with $promise
//dialog.close(true);
};

但我仍然没有得到真实的回应

最佳答案

看到这个 fiddle :http://jsfiddle.net/moderndegree/Kn3Tc/

angular.module('myApp', ['ngResource']).
factory('myService', function($http, $resource, $log){
return $resource('/', {}, {
get: {
method: 'GET',
transformRequest: [function(data, headersGetter){
// you can examine the raw request in here
$log.info(data);
$log.info(headersGetter());
}].concat($http.defaults.transformRequest),
transformResponse: [function (data, headersGetter) {
// you can examine the raw response in here
$log.info(data);
$log.info(headersGetter());
return {tada:"Check your console"};
}].concat($http.defaults.transformResponse)
}
});
}).
controller('myController', function(myService, $scope, $resource, $http, $log) {
$scope.results = myService.get();
});

To globally augment or override the default transforms, modify the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties.



在此处阅读更多信息: http://docs.angularjs.org/api/ng .$http

关于AngularJS 与 ngResource : How to check exact response from server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17417533/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com