Get AP data

更新时间:
复制 MD 格式

You can use this API to retrieve data from unified storage. This API supports only the string data type.

Note

If data is stored on an iOS client directly by the native client, you must use the setString API to store the data. Otherwise, the call to this API fails.

Using the getAPDataStorage API

AlipayJSBridge.call('getAPDataStorage', {
  type: "common",
  business: "customBusinessKey",
  key: "customKey",
}, function(result) {
  alert(JSON.stringify(result));
});

Code example

<button id="J_saveDataBtn" class="btn">Save Data</button>
<button id="J_getDataBtn" class="btn">View Data</button>
<button id="J_removeDataBtn" class="btn">Delete Data</button>

<script>
function ready(callback) {
  // If AlipayJSBridge is already injected, call the callback directly.
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // If not, listen for the AlipayJSBridgeReady event.
    document.addEventListener('AlipayJSBridgeReady', callback, false);
  }
}
ready(function() {
  document.querySelector('#J_saveDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('setAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey",
      value: "customValue"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);

  document.querySelector('#J_getDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('getAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);

  document.querySelector('#J_removeDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('removeAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);
}, false);
</script>

API reference

AlipayJSBridge.call('getAPDataStorage', {
  type, business, key
});

Request parameters

Property

Type

Description

Required

Default value

type

string

The storage dimension. Valid values are user for user-specific storage and common for common storage. The default is common.

N

"common"

business

string

A custom business identity. This identity must be consistent with the one used in the client-side access code. The default value is NebulaBiz.

On Android, this identity corresponds to the GROUD_ID passed when an APSharedPreferences instance is created.

No

""

key

string

The key for the custom data.

Yes

""

Response parameters

The callback function receives the result: {data} parameter.

Property

Type

Description

data

string

The data.

errorMessage

string

The data is not found.

Error codes

Error code

Description

11

The data is not found.