本文介绍了如何检查对象是否是jQuery Xhr对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我处于这样一种情况,我想检查对象是否是jQuery XHR(jqXHR - 例如由.ajax()请求返回的) - 而不仅仅是常规的Deferred对象。

Basically, I am in a situation where I would like to check if an object is a jQuery XHR (jqXHR -- such as is returned by .ajax() requests) -- not just a regular Deferred object.

我试过检查它是否是$ .Deferred的实例,所以它略有不同:

I have tried checking if it's an instance of $.Deferred so it's slightly different:

xhrObj instanceOf $.Deferred       //false

我没有在文档中看到或代码是什么构造函数......无论如何要这样做?

And I don't see in the docs or code what constructor it is... Anyway to do this?

推荐答案

您可以检查延迟应该具有的任何属性,例如已完成失败始终等。

You could check for any property that a deferred should have, such as done, fail, always, state, etc.

已完成似乎是延期应该有的东西(承诺也有),这在其他jQuery对象中并不常见。

done seems like something a deferred should have (which promises also have), that isn't very common among other jQuery objects.

var def = new $.Deferred();

if ('done' in def)

并检查它是否是承诺

var ajax = $.ajax({
    url : 'something'
})

isPromise = 'abort' in ajax;

这篇关于如何检查对象是否是jQuery Xhr对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:07