https://api.jquery.com/jQuery.post/#jQuery-post-url-data-success-dataType

jQuery.post( url [, data ] [, success ] [, dataType ] )

参数:

  • url :请求服务器的url路径
  • data :发送到服务器的数据,格式是键值对key=value&key=value
  • success :如果请求成功, ajax自动执行这个回调函数
  • dataType:从服务器期望得到的数据的类型。默认值:智能猜测(xml,json,script,text,html)

示例:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <input id="btn" type="button" value="ajax异步请求服务器">
  <div id="myDiv"></div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"
    integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>

  <script>
    $('#btn').click(() => {
      // 发送ajax post请求
      $.post(
        // url
        'http://localhost:8080/jsajaxproject/jquery_post',
        // 发送到服务器的数据
        'username=hary&age=18',
        // 成功以后的回调函数
        (data) => {
          $('#myDiv').html(data)
        },
        // 期望服务器返回的数据类型
        'text'
      )
    })
  </script>
</body>

</html>

在这里插入图片描述

点击按钮:
在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐