javascript JQuery:将 GET URL 转换为 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9748593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:42:17  来源:igfitidea点击:

JQuery: Convert GET URL to POST

javascriptjqueryformspostget

提问by ottsch

what's the easiest way to convert a GET URL string to POST in jQuery?

在 jQuery 中将 GET URL 字符串转换为 POST 的最简单方法是什么?

e.g. I want the params of a link

例如我想要一个链接的参数

    <a href="/somepage?x=1&amp;y=3" id="postlink">link</a>

to be submitted as POST onclick if javascript is activated. No AJAX, just normal form submitting.

如果 javascript 被激活,则作为 POST onclick 提交。没有 AJAX,只是普通的表单提交。

Any Ideas?

有任何想法吗?

Thanks, Hannes.

谢谢,汉尼斯。

采纳答案by Sergey Toy

I just write this code, check please, may be it helpful http://jsfiddle.net/AEwxt/

我只是写了这段代码,请检查,可能有帮助http://jsfiddle.net/AEwxt/

$('#postlink').click(function() {
    var p = $(this).attr('href').split('?');
    var action = p[0];
    var params = p[1].split('&');
    var form = $(document.createElement('form')).attr('action', action).attr('method','post');
    $('body').append(form);
    for (var i in params) {
        var tmp= params[i].split('=');
        var key = tmp[0], value = tmp[1];
        $(document.createElement('input')).attr('type', 'hidden').attr('name', key).attr('value', value).appendTo(form);
    }
    $(form).submit();
    return false;
});

回答by mgraph

demo : http://jsfiddle.net/w5WA6/

演示:http: //jsfiddle.net/w5WA6/

function getUrlVars(_url)
{
    var vars = [], hash;
    var hashes = _url.slice(_url.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];

    }

    return vars;
}

var arVars = getUrlVars($("#test").attr("href"));
for(var i=0;i<arVars.length;i++){
    //put var in input for submition you can change type by `hidden`
    $("#myForm").append("<input type='text' value='"+arVars[arVars[i]]+"' name='"+arVars[i]+"'/>");
}
?

回答by SPS

Modified Sergey's code a little, so that when the user 'hovers' on the link, it does not display the get request.

稍微修改了 Sergey 的代码,这样当用户在链接上“悬停”时,它不会显示获取请求。

Link:

关联:

<a href="#" postURL="/tpl/schRosterSearchSubmit.do?searchReci.uniqueId=&searchReci.studentId=&searchReci.schoolId=3205&searchReci.name.firstName=&searchReci.name.lastName=&searchReci.gender=&searchReci.rdobDay=&searchReci.rdobMonth=&searchReci.rdobYear=&searchReci.rssn1=&searchReci.rssn2=&searchReci.rssn3=&searchReci.phoneNumber=&searchReci.citzOrResAlien=&searchReci.country.isoCountryCode=&searchReci.address.street1=&searchReci.address.street2=&searchReci.address.city=&searchReci.address.state=&searchReci.address.zip=&searchReci.address.zipExt=&searchReci.parentN.firstName=&searchReci.parentN.lastName=&searchReci.parentEmail=&searchReci.parentMAddr.street1=&searchReci.parentMAddr.street2=&searchReci.parentMAddr.city=&searchReci.parentMAddr.state=&searchReci.parentMAddr.zip=&searchReci.parentMAddr.zipExt=&searchReci.parentPAddr.street1=&searchReci.parentPAddr.street2=&searchReci.parentPAddr.city=&searchReci.parentPAddr.state=&searchReci.parentPAddr.zip=&searchReci.parentPAddr.zipExt=&searchReci.pdobDay=&searchReci.pdobMonth=&searchReci.pdobYear=&searchReci.pssn1=&searchReci.pssn2=&searchReci.pssn3=&searchReci.parentCitzOrResAlien=&searchReci.parentCountry.isoCountryCode=&searchReci.addrUpdInd=&searchReci.inactiveDtDay=&searchReci.inactiveDtMonth=&searchReci.inactiveDtYear=&searchReci.ncoaUpdInd=&searchReci.badAddrInd=&branding=upromise&acct=XXXX&schRosterPager.offset=50" class="rnavLink">2nd Page results</a>

Converting to POST onclick:

转换为 POST onclick:

<script type="text/javascript">
$(document).ready(function() {
    $('.rnavLink').click(function() {
        var p = $(this).attr('postURL').split('?');
        var action = p[0];
        var params = p[1].split('&');
        var form = $('<form/>', {action:action, method:'post'}).appendTo('body');
        for (var i in params) {
            var tmp = params[i].split('=');
            var key = tmp[0], value = tmp[1];
            $('<input/>', {type:'hidden', name:key, value:value}).appendTo(form);
        }
        $(form).submit();
        return false;
    });
});

回答by badunk

You can send form input using HTML <form method="POST">

您可以使用 HTML <form method="POST"> 发送表单输入

While I'm not entirely sure of the rationale behind this, if you must enforce the javascript part, simply surround it the form with <form method="GET"> with a <noscript> tag.

虽然我不完全确定这背后的基本原理,但如果您必须强制执行 javascript 部分,只需用 <form method="GET"> 和 <noscript> 标记将其包围在表单中。