除了向它发出请求之外,有没有办法检查 API 是否可消耗?
Is there a way to check if API is consumable other than making a request to it?
我正在实施某种空闲计时器,一旦计时器结束,我将调用 API 并更新数据。为此,我需要确保 API 仍然可用。除了向它发出实际请求之外,还有什么方法可以查看 API 是消耗品吗?
如果您是 API 的作者,您可以为此创建一个端点。
示例:HEAD /api/amionline
。使用 HEAD
动词是因为它表示没有请求主体,而且除了获得 200 OK
.
之外几乎没有什么重要的
JS 客户端中使用 jQuery 的示例实现(未测试):
var amIOnline = function(opts) {
$.ajax({
type: 'HEAD',
url: '/api/amionline'
}).then(function() {
opts.yep();
}, function(xhr) {
// XHR is the response object containing info about the failed request.
opts.nope(xhr);
});
};
// Usage:
amIOnline({
yep: function() {
console.log('Connected! Aw yeah!');
},
nope: function(xhr) {
console.log('Not connected. XHR:', xhr);
}
});
我正在实施某种空闲计时器,一旦计时器结束,我将调用 API 并更新数据。为此,我需要确保 API 仍然可用。除了向它发出实际请求之外,还有什么方法可以查看 API 是消耗品吗?
如果您是 API 的作者,您可以为此创建一个端点。
示例:HEAD /api/amionline
。使用 HEAD
动词是因为它表示没有请求主体,而且除了获得 200 OK
.
JS 客户端中使用 jQuery 的示例实现(未测试):
var amIOnline = function(opts) {
$.ajax({
type: 'HEAD',
url: '/api/amionline'
}).then(function() {
opts.yep();
}, function(xhr) {
// XHR is the response object containing info about the failed request.
opts.nope(xhr);
});
};
// Usage:
amIOnline({
yep: function() {
console.log('Connected! Aw yeah!');
},
nope: function(xhr) {
console.log('Not connected. XHR:', xhr);
}
});