目录

Why are the obtained parameters incomplete when launching an ASCF atomic service with parameters containing a question mark (?)?

Context

Symptom

A developer creates a page with route pages/h5/index to load different HTML5 links. These HTML5 links are passed through custom parameters, and the page route is separated from custom parameters by ?. If the value of the custom parameter is also a URL containing ?, the content following ? in the URL will be missing.

For example, if you configure custom parameter ascfPara={"path": "pages/h5/index?url=https://www.example.com?param=value&param1=value1"}, only url=https://www.example.com can be obtained from the URL in the onLoad callback of the pages/h5/index page, and param and param1 cannot be obtained.

Cause

The custom parameter url is an HTML5 link that carries its own query arguments (with ?). When the URL is embedded as a value into the outer path, two ? characters appear in the path string. ASCF extracts the query string using path.split('?')[1]. This logic can only obtain the content following the first ? and cannot distinguish the boundary between the outer parameters and the embedded URL's own parameters. As a result, the query parameters of the embedded URL are truncated or incorrectly parsed, leading to parameter loss.

Solution

When configuring ascfPara.path, it is not recommended that the parameter values contain key characters such as ? and &, as these will cause incorrect parameter truncation.

If these characters are required, you must first encode the parameter value using encodeURIComponent and then encode the entire ascfPara value using encodeURIComponent to produce a double-encoded string. ASCF decodes the entire ascfPara value only once when processing parameters, so the nested parameters can be parsed correctly.

Example

  1. Encode the url value https://www.example.com?param=value&param1=value1 using encodeURIComponent to obtain https%3A%2F%2Fwww.example.com%3Fparam%3Dvalue%26param1%3Dvalue1.

  2. Encode the ascfPara.path value using encodeURIComponent to obtain pages%2Fh5%2Findex%3Furl%3Dhttps%253A%252F%252Fwww.example.com%253Fparam%253Dvalue%2526param1%253Dvalue1.

  3. Retrieve parameters in onLoad.

    // pages/h5/index.js
    Page({
      onLoad(options = {}) {
        let url = options.url;
        if (url) {
          url = decodeURIComponent(url);
          // https://www.example.com?param=value&param1=value1
          console.info('url', url);
        }
      },
    })