This article introduces JSSDK for public account development
Overview
JS-SDK is an internal Web development kit provided by the public platform for web developers.
Through the use of JS-SDK, Web developers can take efficient use of pictures, images, voice, location and other mobile phone system capabilities, at the same time, you can directly use the unique capabilities such as sharing, scanning, card coupons, and payment to provide users with a better web experience.
This document Introduces Web developers how to use JS-SDK and related precautions.
How to use JSSDK Step 1: bind a domain name
Log on to the public platform and go to "function settings" of "public Account Settings" and enter "JS interface security domain name ".
Note: after logging on, you can view the corresponding interface permissions in the "Developer Center.
Step 2: Introduce the JS file
Introduce the following JS file on the page that needs to call JS interface (support https): http://www.php.cn/
Note: supports loading using AMD/CMD standard modules.
Step 3: inject the permission verification configuration through the config interface
All pages that require JS-SDK must first inject configuration information, otherwise it will not be called (the same url only needs to be called once, the SPA web app that changes the url can be called every time the url changes. Currently, the Android client does not support the new H5 feature of pushState, therefore, using pushState to implement web app pages will cause signature failure, which will be fixed in Android6.2 ).
Wx. config ({debug: true, // enable the debugging mode. The Returned values of all called APIs are displayed in the client alert. to view the input parameters, you can open them on the pc, the parameter information is output through log and printed only on the pc end. AppId: '', // required. the unique identifier timestamp:, // required. the timestamp of the generated signature is nonceStr:''. // required, sign the random string signature: '', // required. for details, see Appendix 1 jsApiList: [] // required. list of JS interfaces to be used, for a list of all JS interfaces, see Appendix 2 });Step 4: verify the processing by using the ready interface
Wx. ready (function () {// after the config information is verified, the ready method is executed. all interfaces must be called after the config interface obtains the result. config is a client-side asynchronous operation, therefore, if you need to call the interface when loading the page, you must place the interface in the ready function to ensure proper execution. For interfaces that are called only when triggered by the user, you can directly call them without putting them in the ready function .});
Step 5: handle failure verification through the error interface
Wx. error (function (res) {// if the config information fails to be verified, the error function is executed. if the signature expires, the verification fails. you can view the error information in debug mode of config, you can also view it in the returned res parameter. for SPA, you can update the signature here .});Interface call description
All interfaces are called through wx objects (jWeixin objects can also be used). a parameter is an object. in addition to the parameters required for each interface, the following common parameters are provided:
-
-
Success: The callback function executed when the API call is successful.
-
Fail: The callback function executed when the interface call fails.
-
Complete: The callback function that is executed when the interface call is completed, whether it is successful or failed.
-
Cancel: the callback function used when you click cancel. This function is only used by some APIs that have canceled the operation.
-
Trigger: method triggered when a button in the Menu is clicked. this method only supports related interfaces in the Menu.
Note: Do not try to use ajax asynchronous requests to modify the content of this share in trigger, because the share operation on the client is a synchronization operation. at this time, no ajax return packet is returned.
All the above functions have a parameter of the object type. besides the data returned by each interface, there is also a general attribute errMsg, whose value format is as follows:
-
When the call is successful: "xxx: OK", xxx indicates the called interface name.
-
When the user cancels the call: "xxx: cancel", xxx indicates the called interface name.
-
When the call fails: the value is an error message.
////// Prepare parameters ///Private void WxSdkPramas (bool isShare) {var jsSdk = new JSSDKHelper (); // Obtain the timestamp var timestamp = JSSDKHelper. getTimestamp (); // Obtain the random code var nonceStr = JSSDKHelper. getNoncestr (); var appId = WeiXinAppId; var appSecret = WeiXinAppSecret; // Obtain the ticket var jsTicket = JsApiTicketContainer. tryGetTicket (appId, appSecret); // obtain the signature var signature = jsSdk. getSignature (jsTicket, nonceStr, timestamp, Request. url. absoluteUri); ViewData ["AppId"] = appId; ViewData ["Timestamp"] = timestamp; ViewData ["NonceStr"] = nonceStr; ViewData ["Signature"] = signature ;}
- The following is the js code:
Demonstration of public account JSSDK