我怎样才能 POST 一个参数,使其不出现在查询字符串中,以确保用户不能直接在 URL 中更改值?

How can I POST a parameter so that it doesn't show up in the querystring, to make sure users can't change the value directly in the URL?

我有一个分页的搜索结果页面,当前页码包含在查询字符串中,如下所示:

mysite.com/search/database-search?name=smith&page=10

我不希望用户能够直接在 url 中更改页码,他们应该只能通过单击页面按钮来更改页面。所以我需要从查询字符串中删除页面,但仍将页码作为参数传递以获得适当的结果页面。

我认为我可以通过使用 POST 而不是 GET 来完成此操作;我的页面按钮最初设置为 "href=/search/database-search?name=smith&page=xx," 的链接,但我认为如果我使用表单提交页码,那将传递页码而不在查询字符串中显示它。

这是我试过的代码:

function postToPage(page) {
    debugger;
    var url = window.location.href;
    var newForm = $("<form>", {
        "action": url
    }).append($("<input>", {
        "name": "page",
        "value": page,
        "type": "hidden"
    }));
    newForm.submit();
}

<a onclick="postToPage($(this).text());" href="javascript:void(0)" class="page-button">@pageNum</a>

但是,它并没有像我预期的那样工作。当我点击页面按钮时,url 中的查询字符串被忽略,"page" 仍然出现在查询字符串中,所以结果与我想要完成的有点相反;我失去了我需要的查询参数,并且 "page" 是唯一仍然包含在查询字符串中的东西。

当我点击页面按钮时,我被带到“/search/database-search?page=x”

有什么方法可以将页面用作查询参数而不显示在 URL

编辑:我将表单方法更改为 "POST," 但 "page" 变量丢失了。现在包含当前查询字符串中的参数,但未使用页面值。

您似乎没有将表单的方法设置为 POST。

function postToPage(page) {
    debugger;
    var url = window.location.href;
    var newForm = $("<form>", {
        "action": url,
        "method": "POST" // set form to POST, instead of the default GET
    }).append($("<input>", {
        "name": "page",
        "value": page,
        "type": "hidden"
    }));
    newForm.submit();
}