All Products
Search
Document Center

Mobile Platform as a Service:H5 JS programming

Last Updated:Oct 30, 2023

Overview

At present, JS is widely used for mobile frontend coding. mPaaS provides another mobile Web solution: HTML5 Container. HTML5 is based on Android and iOS, see mPaaS overview for more information about how access HTML5.

After the client accesses HTML5, the frontend can conveniently use the gateway:

  • Encapsulate the communication between client and server via dynamic agent.

  • If the server and client have defined the same interfaces, then the codes are automatically generated by server and exported to the client for use.

  • Uniformly process RpcException by using pop-up dialog, toast, and so on.

Prerequisites

The Android/iOS client has accessed HTML5 Container.

Generate JS codes

When application accesses backend services from mobile gateway console, the JS calling codes of RPC can be automatically generated on the console. See Mobile Gateway Service > Generate codes for more information.

According to the agreed interface parameter, the following template codes will be generated for each API:

var params = [{
    "_requestBody":{"userName":"", "userId":0}
}]
var operationType = 'alipay.mobile.ic.dispatch'

AlipayJSBridge.call('rpc', {
  operationType: operationType,
  requestData: params,
  headers:{}
}, function (result) {
  console.log(result);
});

When the frontend needs to use RPC, it uses the template mentioned above and fills the called request parameters into the template.

Usage instruction

The codes that JS uses to call RPC are as follows:

AlipayJSBridge.call('rpc', {
  operationType: 'alipay.client.xxxx',
  requestData: [],
  headers:{}
}, function (result) {
  console.log(result);
});

Parameter description

Name

Type

Description

Optional

Default value

operationType

string

RPC service name

N

requestData

array

Parameter of RPC request, developers need to build it on their own according to the actual RPC interface

N

headers

dictionary

The headers of RPC request

Y

{}

gateway

string

Gateway address

Y

alipay gateway

compress

boolean

Whether or not the request gzip compressing is supported

Y

true

disableLimitView

boolean

Whether or not to disable the auto pop-up of traffic limit window when the traffic of RPC gateway is limited

Y

false

Result

Result

Type

Description

result

dictionary

The result of RPC response (The string values of non-dictionary structure will be put into a dictionary structure, the key is resData)

Errors

Error

Description

10

Network error

11

Request timeout

Others

Defined by mobilegw