我目前正在尝试找出从第一个 AJAX 调用返回 ID 并将该 ID 传递到第二个 AJAX 调用的方法。我已经设置了
let vm = this
。将 vm.data.id
从 results.id
传递到第二个调用的正确方法是什么?
vm.retrieveId = () => {
$.ajax
({
type: "GET",
url: url,
dataType: 'jsonp',
success: function (res) {
vm.data = res;
console.log(vm.data);
}
});
};
vm.getPhotos = () => {
$.ajax
({
type: "GET",
url: "http://webbtelescope.org/api/v3/image/" + vm.data.id,
dataType: 'jsonp',
success: function (res) {
let results = res;
vm.photos = results;
}
});
}
vm.retrieveId = (callback) => {
$.ajax({
type: "GET",
url: url,
dataType: 'jsonp',
success: (res) => res ? callback(res) : console.error("No data returned"),
error: (xhr, status, error) => console.error("Error retrieving ID:", status, error)
});
};
vm.getPhotos = (id) => {
if (!id) return console.error("Invalid ID");
$.ajax({
type: "GET",
url: `http://webbtelescope.org/api/v3/image/${id}`,
dataType: 'jsonp',
success: (res) => res ? (vm.photos = res) : console.error("No photos returned"),
error: (xhr, status, error) => console.error("Error retrieving photos:", status, error)
});
};
vm.retrieveId((data) => data.id ? vm.getPhotos(data.id) : console.error("No valid ID found"));