问题描述
有没有什么方法可以隐藏URL参数,然后用代码读取隐藏的参数?例如,我的页面中有这个:example.com/transaction.html?price=10".
Is there any method to hide URL parameters and then read the hidden parameters with code? For example I have this in my page:"example.com/transaction.html?price=10".
我尝试使用 Base64 进行编码,但它没有按预期工作(编码有效,解码无效),所以我想隐藏 price=10 或 transaction.html?price=10.
I have tried to encode with Base64, but it doesn't work as expected (encoding works, decoding not), so I want to hide either the price=10 or transaction.html?price=10.
我听说 AngularJS 可以隐藏参数,但它可以读取隐藏"的参数或者它是如何工作的?
I heard that AngularJS could hide parameters, but can it read "hidden" parameters or how does it work?
提前致谢!
function getUrlVar() {
var result = {};
var location = window.location.href.split('#');
var parts = location[0].replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {
result [key] = value;
});
return result;
}
推荐答案
如果不想在地址栏中向用户展示查询参数,可以使用Javascript或者jquery轻松实现也需要切换到角度只是为了这个.
If you don't want to show the query parameters to the users in the address bar, you can easily achieve it using Javascript or jquery also need to switch to angular just for this.
你可以做的是代替使用 </a>
获取该查询的数据,该查询会将用户重定向到链接说 www.something.com?price=10
而不是您可以做的就是使用一个按钮
<button type="button" onclick="fetch()">获取数据</button>
并且在您的 Javascript 文件中,您可以通过这种方式发出 GET 或 POST
请求,这些您想要隐藏的参数将不会显示,只是这些将在后台发送到服务器,而无需更改您的地址.
为了制作这个 GET 请求
,你可以做一些事情,比如假设你从文本字段发送数据:
$("#button").click(function() {
$.ajax({
type: "POST",
url: "www.something.com/",
data: {
price: $("#price")
},
success: function(data) {
// do something with the data you received}
});
})
<input type=text id="price" />
<button id="button">Send</button>
Now you got the data in background without showing the parameters to the user in their address bar.
这篇关于隐藏 URL 参数并读取它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!