要使用JavaScript读取GET请求,您可以使用以下方法:
// 假设URL为:https://example.com?key1=value1&key2=value2
const urlParams = new URLSearchParams(window.location.search);
const key1 = urlParams.get('key1'); // 获取key1的值,即value1
const key2 = urlParams.get('key2'); // 获取key2的值,即value2
const url = window.location.href;
const regex = /[?&]([^=]+)=([^&#]*)/g;
const params = {};
let match;
while ((match = regex.exec(url))) {
params[match[1]] = match[2];
}
const key1 = params['key1']; // 获取key1的值,即value1
const key2 = params['key2']; // 获取key2的值,即value2
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const key1 = urlParams.get('key1'); // 获取key1的值,即value1
const key2 = urlParams.get('key2'); // 获取key2的值,即value2
请注意,这些方法仅适用于浏览器环境。如果您需要在Node.js环境中读取GET请求,您需要使用第三方库(如Express.js)来处理HTTP请求。
领取专属 10元无门槛券
手把手带您无忧上云