# 4.jQuery的AJAX

# 4.1 get 请求

$('button').click(function(){
	// 三/四个参数 url 参数 回调函数(响应体) 响应体类型
	$.get('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
        console.log(data)
    }, 'json');
})

# 4.2 post请求

$('button').click(function(){
	$.post('http://127.0.0.1:8000/jquery-server', {a:100, b:200}, function(data){
        // 回调函数
        console.log(data)
    })
})

# 4.3 通用方法ajax

$('button').click(function(){
	// 三/四个参数 url 参数 回调函数(响应体)
	$.ajax({
        //url
        url: 'http://127.0.0.1:8000/jquery-server',
        // 参数
        data: {a:100, b:200},
        // 请求类型
        type: 'GET',
        // 响应体类型
        dataType: 'json',
        // 成功的回调
        success: function (data) {
            console.log(data);
        },
        // 超时时间
        timeout: 2000,
        // 失败的回调
        error: function () {
            console.log('error');
        },
        // 头信息
        headers: {
            c:300,
            d: 400
        }
    })
更新时间: 6/29/2021, 4:20:37 PM