반응형
$resource 콜백(오류 및 성공)
AngularJS 1.1.3을 사용하여 새로운 $리소스를 약속과 함께 사용합니다.
어떻게 하면 콜백을 받을 수 있을까요?$http와 같은 방법으로 시도했습니다.
$resource.get('...').
success(function(data, status) {
alert(data);
}).
error(function(data, status) {
alert((status);
});
하지만 '성공'도 없고 '오류' 기능도 없습니다.
저도 해봤어요.
$resource.get({ id: 10 },function (data) {
console.log('success, got data: ', data);
}, function (err) {
alert('request failed');
});
이는 항상 "성공, 데이터 획득"을 인쇄합니다.반환율이 404인 경우에도 마찬가지입니다.
감 잡히는 게 없어요?
감사해요.
각도 리소스와 각도 1.2에 대한 PR 시점에서는 각도가 성공/오류 검사를 수행하는 더 간단한 방법으로 전환됩니다.콜백 또는 $then 메서드를 부가하는 대신 Resource.get(..)과 instance.get() 모두 $promise 메서드를 지원하며, 이는 자연스럽게 양쪽 모두에 대한 약속을 반환합니다.
각 1.2부터는 $promise 기능이 활성화됩니다.$promise 변경 사항
「취득」요구를 다음의 방법으로 변경합니다(원래의 예는 angularjs.org 의 1 페이지에 게재되어 있습니다).
factory('Project', function($resource) {
var Project = $resource('https://api.mongolab.com/api/1/databases' +
'/youraccount/collections/projects/:id',
{ apiKey: 'yourAPIKey' }, {
update: { method: 'PUT' }
}
);
Project.prototype.update = function(cb) {
return Project.update({id: this._id.$oid})
.$promise.then(
//success
function( value ){/*Do something with value*/},
//error
function( error ){/*Do something with error*/}
)
};
Project.prototype.destroy = function(cb) {
return Project.remove({id: this._id.$oid})
.$promise.then(
//success
function( value ){/*Do something with value*/},
//error
function( error ){/*Do something with error*/}
)
};
return Project;
});
컨트롤러의 다른 곳에서 리소스 "프로젝트" 인스턴스를 인스턴스화하여 동일한 인터페이스를 사용하여 성공과 오류를 수행할 수 있습니다.
var myProject = new Project();
myProject.$get({id: 123}).
.$promise.then(
//success
function( value ){/*Do something with value*/},
//error
function( error ){/*Do something with error*/}
)
var MyResource = $resource("/my-end-point/:action", {}, {
getSomeStuff: { method:"GET", params: { action:"get-some-stuff" }, isArray: true },
getSingleThing: { method:"GET", params: { action:"get-single-thing" }, isArray: false }
});
function MyController(MyResource) {
var itemList = MyResource.getSomeStuff({}, function success() {}, function err() {});
// will call: /my-end-point/get-some-stuff
// will be array. each object is resource's instance
var item = MyResource.getSingleThing({id:123}, function success() {}, function err() {});
// will call: /my-end-point/get-single-thing?id=123
// will be object. an instance of resource
}
또한 docs의 예를 참조하십시오.ngResource
두 가지 방법
var resource = $resource("");
resource.$promise.then(function(data){
// do stuff success
}, function(error){
//do stuff error
});
다른 방법이다.
var resource = $resource("");
resource({}, function success(data){
//do stuff
}, function error(error){
//do stuff
}
언급URL : https://stackoverflow.com/questions/15531117/resource-callback-error-and-success
반응형
'programing' 카테고리의 다른 글
| Spring Boot @Response Body에서 404 응답 상태를 반환하는 방법 - 메서드 반환 유형은 Response입니까? (0) | 2023.04.03 |
|---|---|
| Spring Boot에서 Web Client Mono를 사용하여 API 응답 오류 메시지 가져오기 (0) | 2023.04.03 |
| wp rest api는 메타로 게시물을 가져옵니다. (0) | 2023.04.03 |
| Jackson이 단일 JSON 개체를 하나의 요소가 있는 배열로 해석하도록 합니다. (0) | 2023.04.03 |
| jQuery에서 줄 바꿈이 포함된 JSON 형식의 텍스트를 검색할 때 문제가 발생함 (0) | 2023.04.03 |