get()请求
$.get(url,回调函数(请求的返回值))
$.get('http://wkt.shangyuninfo.cn/weChat/applet/course/banner/list?number=4', function (res) {
res.data.forEach(function (item, index) {
console.log(item)
$('div').append(`<img src="${item.imgUrlPc}" alt=""`)
})
})
post()请求
$.post(url,参数对象,回调函数) 默认请求头 application/x-www-form-urlencoded
var baseUrl = 'http://wkt.shangyuninfo.cn'
$.post(baseUrl + '/weChat/applet/course/list/type', { type: 'free', pagnNum: 1, pageSize: 5 }, function (res) {
console.log(res)
})
get()和post()的区别
参数位置的不同:
get()请求:1.get把请求参数放在url上,集http协议头上;2、因此get比post更不安全,不能用来传递敏感信息;3、get请求参数会被完整保留在浏览器历史记录里。
post()请求:1、post把请求参数放在Request body请求体中;2、post比get安全;3、post中参数不会保留在浏览器历史记录中。
传输的数据大小不同:
get()请求:get一般来说提交的数据最大是2k。
post()请求:post理论上没有限制。
传输的数据类型不同:
get()请求:get只接受ASCll字符。
post()请求:post没有限制。
数据包不同:
get()请求:get产生一个TCP数据包。
post()请求:post产生两个TCP数据包.
ajax()请求
$.ajax(url, type,传的参数) type默认类型是get
$.ajax({
url: 'http://wkt.shangyuninfo.cn/weChat/applet/subject/list',
type: 'post',
data: JSON.stringify({ enable: 1 }),
complete: function (comp) {
console.log(comp)
},
success: function (res) {
console.log(res)
},
error: function (error) {
console.log(error);
},
contentType: 'application/json'
})