IndexedDB api中的IDBRequest接口提供了根据绑定事件处理函数访问结果集的方法。其中结果集来自对数据库和数据库对象发起的异步查询。对数据库的读写操作都是通过request的方式来实现。


继承自: EventTarget

About this document

This document was last updated on August 17, 2012 and follows the W3C Specifications (Editor's Draft) drafted on July 24, 2012. It has not yet been verified.






var request ='数据库名称');
request.onsuccess = function(event) {
        var db = this.result;
        var transaction = db.transaction([]);
// "readonly" is the default option; 
// when data will be added to the database use "readwrite".
        var curRequest = transaction.objectStore('ObjectStore Name').openCursor();
        curRequest.onsuccess = ...;
request.onerror = function(event) {
request.onupgradeneeded= function(event) {
         // changing objectStore data is done here, as opposed to a transaction enum:


Attribute Type Description
result readonly any

Returns the result of the request.

If the the request failed and the result is not available, the InvalidStateError exception is thrown.

error readonly DOMError

The following error codes are returned under certain conditions:

  • AbortError — If you abort the transaction, then all requests still in progress receive this error.
  • ConstraintError — If you insert data that doesn't conform to a constraint. It's an exception type for creating stores and indexes. You get this error, for example, if you try to add a new key that already exists in the record.
  • QuotaExceededError — If you run out of disk quota and the user declined to grant you more space.
  • UnknownError — If the operation failed for reasons unrelated to the database itself. A failure due to disk IO errors is such an example.
  • NoError — If the request succeeds.
  • VersionError — If you try to open a database with a version lower than the one it already has.

In addition to the error codes sent to the IDBRequest object, asynchronous operations can also raise exceptions. The list describes problems that could occur when the request is being executed, but you might also encounter other problems when the request is being made. For example, if the the request failed and the result is not available, the InvalidStateError exception is thrown.

source readonly Object

The source of the request, such as an Index or a ObjectStore. If no source exists (such as when calling, it returns null.

transaction readonly IDBTransaction The transaction for the request. This property can be null for certain requests, such as for request returned from (You're just connecting to a database, so there is no transaction to return).
readyState readonly enum

The state of the request. Every request starts in the pending state. The state changes to done when the request completes successfully or when an error occurs.

onerror Function The event handler for the error event.
onsuccess Function The event handler for the success event.


readyState constants

已废弃 Gecko 25 (Firefox 25 / Thunderbird 25 / SeaMonkey 2.22)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

These constants are no longer available. You should use directly the string constants instead. (bug 887524)

Constant Value Description
DONE "done" The request has completed or an error has occurred. Initially false
LOADING "pending" The request has been started, but its result is not yet available.

Event handlers

Event handler Event handler type
onerror error
onsuccess success

Derived interface

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12 -webkit 4.0 (2.0) 未实现 未实现 未实现
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 未实现 6.0 (6.0) ? 未实现 未实现



 此页面的贡献者: teoli, thankwsx
 最后编辑者: teoli,