ajax 封装了一些方法 ajax get post getJSON getScript
$(function () {
/*
ajax 封装了一些方法 ajax get post getJSON getScript
get()的参数:url data callback type
*/
$.get("../data/stu.json", function (res) {
console.log(res);
},"json");
});
post 与get 相同
$(function () {
$.post("../data/stu.json", function (res) {
console.log(res);
},"json");
});
ajax
设置dataType:"json",
不用JSON.parse
cros跨域或者不跨域的代码:如下 如是jsonp跨域,则dataType:"jsonp",
$(function () {
$.ajax({
method:"post",
url:"../data/student.txt",
success: function (res) {//请求成功的回调函数
console.log(JSON.parse(res));
},
error: function (res) {
},
async:true
});
}