JSAPI是支付宝客户端通过bridge机制,允许前端H5页面通过特定的JS方法,可以直接调用对应的支付、拍照、分享、弹出浮层之类的Native功能。
但需要注意的是由于bridge的机制问题,iOS端暂时无法在iframe内正常调用所有的JSAPI,请避免在iframe内使用。
// JSAPI is injected in async way
// You should call AlipayJSBridge after event 'AlipayJSBridgeReady' is ready
function ready(callback) {
// Call AlipayJSBridge right away if it is already injected
if (window.AlipayJSBridge) {
callback && callback();
} else {
// Should listen to event 'AlipayJSBridgeReady' to fire
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function () {
AlipayJSBridge.call('toast', {
content: 'hello'
});
});
The param is a dictionary for callback function of jsAPI, it may contain a special field called ‘error’, ‘error’ code should tell why the calling is failed, as below:
{
error: 1,
errorMessage: 'The function is unavailable'
}
Common error code is under 10, should be told by specific function. General common error codes are explained as below:
error | means |
---|---|
1 | The function is unavailable |
2 | Invalid param |
3 | Unknown error |
4 | The calling of function is unauthorized |
包含钱包版本信息,如
"Mozilla/5.0......AlipayDefined(nt:WIFI,ws:320|504|2.0) AliApp(AP/9.6.0.000001) AlipayClient/9.6.0.000001 Language/zh-Hans ProductType/dev"
可通过该环境变量获取当前app的启动参数,如
{"appId": "12345678", "st": "YES", "sb": "NO", "url": "https://m.taobao.com/"}
Warning: 由于iOS容器的某个缓存机制兼容性问题,极端场景下通过startupParams环境变量可能会获取到上一次打开相同页面的值,因此切勿通过启动参数机制来传递重要的自定义业务参数。