Send POST data along when link is clicked without

2019-06-06 22:49发布

I have some regular anchor tag links on my page that open up a popup window which displays whatever GET data I pass through the url:

<a href="javascript:window.open('view.php?data=a%20bunch%20of%20data');">View</a>

Some of this data is really long, and my crappy web host has a very small limit on the size of the GET data that can be passed in a url. Is there a way to do this with POST data instead, without using html forms?

4条回答
来,给爷笑一个
2楼-- · 2019-06-06 23:09

I just ran into the same sort of dilemma and came up with this solution in jQuery:

<a href="#" id="post-link">Example</a>

<script type="text/javascript">
     $(document).ready(function () { 
          $('a#post-link').click(function() {
               $('body').append($('<form/>', {
                    id: 'form',
                    method: 'POST',
                    action: '#'
               }));

               $('#form').append($('<input/>', {
                    type: 'hidden',
                    name: 'field1',
                    value: 'foo'
               }));

               $('#form').append($('<input/>', {
                    type: 'hidden',
                    name: 'field2',
                    value: 'bar'
               }));

               $('#form').submit();

               return false;
          });
     } );
</script>
查看更多
祖国的老花朵
3楼-- · 2019-06-06 23:09

One thing I learned during my long experience with Web development: if you run into limitation from your hosting, host your site somewhere else. Going around hosting limitation - whatever they are - wont't get you very far in the long run. Hosting is cheap, you can get a VPS with generous resources for very reasonable prices nowadays.

It's not worth it.

查看更多
老娘就宠你
4楼-- · 2019-06-06 23:12

you could also use an XMLHttpRequest

http://www.w3schools.com/xml/xml_http.asp

查看更多
女痞
5楼-- · 2019-06-06 23:18

You would need to use forms. However, you could nest your hyperlink in a form:

<form method="post" action="somepage.php" id="form1">
<input type="hidden" name="field1" value="foo" />
<input type="hidden" name="field2" value="bar" />
<a href="somepage.php?field1=foo&field2=bar" onclick="document.getElementById('form1').submit(); return false;">Hyperlink</a>
</form>
查看更多
登录 后发表回答