darecy

html post请求之a标签的两种用法举例

1、使用ajax来发起POST请求
HTML代码如下:

<a href="https://www.cnblogs.com/" class="a_post">发起POST请求</a>

JQuery代码如下:

$(".a_post").on("click",function(event){
    event.preventDefault();//使a自带的方法失效,即无法调整到href中的URL(https://www.cnblogs.com/)
    $.ajax({
           type: "POST",
           url: "url地址",
           contentType:"application/json",
           data: JSON.stringify({param1:param1,param2:param2}),//参数列表
           dataType:"json",
           success: function(result){
              //请求正确之后的操作
           },
           error: function(result){
              //请求失败之后的操作
           }
    });
});
2、使用form表单来发起POST请求
<form id="_form" method="post" action="target">
  <input type="hidden" name="name" value="value" /> 
  <a onclick="document.getElementById(\'_form\').submit();">点击提交</a>
</form>

分类:

技术点:

相关文章:

  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2021-12-09
  • 2021-10-25
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-01-19
  • 2021-11-17
  • 2021-11-27
  • 2021-07-31
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案