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>
点击按钮: