Note: Be sure to introduce related libraries before using Jquery.
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Use Jquery to send GET request
$('button').eq(0).click(function() {
$.get('http://localhost:8000/jquery-server',{
a:20,b:30},function(data) {
console.log(data);
},'json')
});
Use Jquery to send POST request
$('button').eq(1).click(function() {
$.post('http://localhost:8000/jquery-server',{
a:20,b:30},function(data) {
console.log(data);
})
});
Use Jquery for general method requests
// 通用型方法
$('button').eq(2).click(function () {
$.ajax({
url: 'http://localhost:8000/jquery-server',
data: {
a: 20, b: 100 },
dataType: 'json',
type: 'GET',
success: function (data) {
console.log(data);
},
// 超时时间
timeout: 4000,
error: function () {
console.log("网络超时");
},
headers: {
a: 666,
b: 777
}
})
})
- Many of the above attributes can be customized.
- When setting headers, the server must have the following code
response.setHeader('Access-Control-Allow-Headers', '*');