如何用JavaScript返回语句跳转页面并展示Adyen加密返回值
简便实现跳转页面展示JS函数返回值的方案
嘿,针对你用Adyen加密信用卡信息后,想跳转新页面展示JS函数返回值的需求,这里有几个简单直接的实现方案,适配你的场景:
方式1:通过URL查询参数传递(最省心的轻量方案)
当你的JavaScript函数拿到Adyen加密后的返回值后,直接把值拼到新页面的URL里,跳转过去就行,新页面再读取URL参数展示。
举个实际代码例子,假设你的加密函数叫encryptCard():
// 表单提交时的处理函数 function handleSubmit() { // 调用Adyen加密逻辑,拿到返回的加密数据 const encryptedData = encryptCard(); // 跳转到结果页面,把加密值编码后作为URL参数传递 window.location.href = `result.html?encrypted=${encodeURIComponent(encryptedData)}`; }
然后在result.html里,页面加载时读取参数并展示:
window.onload = function() { // 解析URL参数 const params = new URLSearchParams(window.location.search); const encryptedValue = params.get('encrypted'); // 把值展示在页面的容器里(比如你提前写好的div) document.getElementById('result-container').textContent = `加密后的信用卡数据:${encryptedValue}`; };
小贴士:一定要用
encodeURIComponent()处理参数,避免特殊字符导致URL解析出错。如果返回值是JSON对象,先转成字符串JSON.stringify(encryptedData)再编码,新页面用JSON.parse()解析回来就行。
方式2:通过localStorage临时存储(适合复杂数据)
如果加密返回值是结构复杂的对象,或者内容比较大,用URL参数可能不太方便,那就用localStorage临时存一下,跳转后再读取:
// 提交处理函数 function handleSubmit() { const encryptedData = encryptCard(); // 把加密数据转成字符串存到localStorage localStorage.setItem('adyenEncryptedData', JSON.stringify(encryptedData)); // 跳转到结果页面 window.location.href = 'result.html'; }
在result.html里读取并展示:
window.onload = function() { const storedData = localStorage.getItem('adyenEncryptedData'); const encryptedValue = JSON.parse(storedData); // 格式化展示,方便查看 document.getElementById('result-container').textContent = `加密数据:\n${JSON.stringify(encryptedValue, null, 2)}`; // 用完记得删掉,避免数据残留 localStorage.removeItem('adyenEncryptedData'); };
要是你只需要会话内有效,换成
sessionStorage会更合适,它在浏览器关闭后就自动清除了。
方式3:表单隐藏字段提交(适配传统表单流程)
如果你的页面本来就是用表单提交的逻辑,可以把加密后的值放到隐藏字段里,让表单提交到新页面:
<!-- 原表单里加一个隐藏字段 --> <form id="card-form" action="result.html" method="GET"> <!-- 你的信用卡输入字段 --> <input type="text" name="cardNumber" placeholder="卡号" /> <input type="text" name="expiryDate" placeholder="有效期" /> <!-- 隐藏字段用来存加密后的值 --> <input type="hidden" name="encryptedData" id="encrypted-data" /> <button type="submit" onclick="return handleSubmit()">提交</button> </form>
function handleSubmit() { const encryptedData = encryptCard(); // 把加密值编码后放到隐藏字段 document.getElementById('encrypted-data').value = encodeURIComponent(JSON.stringify(encryptedData)); // 返回true允许表单提交,自动跳转到action指定的页面 return true; }
之后在result.html里,不管是用JS读取URL参数,还是后端语言处理表单数据,都能拿到加密值了。
内容的提问来源于stack exchange,提问作者Mr. Kevin
相关产品推荐
相关产品推荐

