Jquery Ajax笔记

$.ajax()返回底层创建的XMLHttoRequest对象.

$.ajax({
           url: './mock-data.json',
method: 'get',
dataType: 'json',
success: function(res) {
console.log(res.data);
for (var i = 0; i < res.data.length; i++) {
str += '<li>' + res.data[i].name + '</li>';
}
$('ul').append(str);
},
error: function(err) {
console.log(err);
} })

Get方法:

$.get() 方法通过 HTTP GET 请求从服务器上请求数据

$.get('./mock-data.json', function(res, status) {
var result = JSON.parse(res);
result = result.data;
})

Post方法:

$.post() 方法通过 HTTP POST 请求向服务器提交数据

$.post('./mock-data.json', {
name: '111'
}, function(res, status) {
alert("数据: " + data + " 状态: " + status);
})
原文地址:https://www.cnblogs.com/qingfengyuan/p/12980713.html