Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何发布一个参数,使其不会显示在查询字符串中,以确保用户不能直接在URL中更改该值?_Javascript_Html_Forms_Post_Parameters - Fatal编程技术网

Javascript 我如何发布一个参数,使其不会显示在查询字符串中,以确保用户不能直接在URL中更改该值?

Javascript 我如何发布一个参数,使其不会显示在查询字符串中,以确保用户不能直接在URL中更改该值?,javascript,html,forms,post,parameters,Javascript,Html,Forms,Post,Parameters,我有一个分页的搜索结果页面,当前页码包含在querystring中,如下所示: mysite.com/search/database search?name=smith&page=10 我不希望用户能够直接在url中更改页码,他们应该只能通过单击页面按钮来更改页面。因此,我需要从querystring中删除页面,但仍然将页码作为参数传递,以获得适当的结果页面 我想我可以通过使用POST而不是GET来实现这一点;我的页面按钮最初设置为带有href=/search/database search?n

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

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

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

我想我可以通过使用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中的查询字符串被忽略,页面仍然显示在查询字符串中,因此结果与我试图实现的相反;我丢失了所需的查询参数,而页面是查询字符串中唯一仍然包含的内容

当我点击页面按钮时,我进入/search/database search?page=x

有没有办法将页面用作查询参数而不在URL中显示


编辑:我将表单方法更改为POST,但页面变量丢失。当前querystring中的参数现在包括在内,但未使用页面值

看起来您没有将表单的方法设置为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();
}

可能的副本谢谢!我知道我遗漏了什么,我不经常使用表格。把他的答案标记为已接受