- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用工具模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - urlSearchParams.set() 方法
URLSearchParams 類的NodeJS urlSearchParams.set() 方法用於設定查詢字串的名稱-值對。
URLSearchParams API 提供了訪問和讀寫 URL 查詢的方法。此類也可用在全域性物件上。
語法
以下是NodeJS URLSearchParams.set() 方法的語法
URLSearchParams.set(name, value)
引數
此方法接受兩個引數。具體說明如下。
name: 指定要設定的引數名稱。
value: 指定要設定的引數值。
返回值
此方法返回 undefined。
示例
如果我們將值傳遞給 NodeJS urlSearchParams.set() 方法的“name”和“value”引數,它將把這些值作為名稱-值對設定到查詢字串。
在下面的示例中,我們嘗試將一些名稱-值對設定到一個空的查詢字串。
const url = require('node:url');
let params = new URLSearchParams('');
console.log('Query string: ' + params);
console.log('Setting name-value pairs to the query string.....');
params.set('txt', 10);
params.set('pdf', 20);
params.set('dcmnt', 30);
params.set('word', 40);
console.log("Query string: " + params.toString());
輸出
執行上述程式後,將生成以下輸出
Query string: Setting name-value pairs to the query string..... Query string: txt=10&pdf=20&dcmnt=30&word=40
示例
如果查詢字串中存在任何預先存在的名稱-值對與 NodeJS set() 方法要設定的名稱匹配,它將刪除所有這些匹配的名稱-值對,並設定新新增的對。
在下面的示例中,我們嘗試新增一個與查詢字串中現有名稱-值對名稱匹配的名稱-值對。
const url = require('node:url');
let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40');
console.log('Query string: ' + params);
console.log('Setting name-value pairs to the query string.....');
params.set('txt', 15);
console.log("Query string: " + params.toString());
輸出
執行上述程式後,將生成以下輸出
Query string: txt=10&pdf=20&dcmnt=30&word=40 Setting name-value pairs to the query string..... Query string: txt=15&pdf=20&dcmnt=30&word=40
示例
如果要由 set() 方法設定的值包含特殊字元,則這些字元將被百分比編碼,並設定到查詢字串。
const url = require('node:url');
let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40');
console.log('Query string: ' + params);
console.log('Setting name-value pairs to the query string.....');
params.set('t`x`t', 15);
console.log("Query string: " + params.toString());
輸出
執行上述程式後,將生成以下輸出
Query string: txt=10&pdf=20&dcmnt=30&word=40 Setting name-value pairs to the query string..... Query string: txt=10&pdf=20&dcmnt=30&word=40&t%60x%60t=15
nodejs_url_module.htm
廣告
