虽然说原生的js代码 封装ajax请求也不麻烦。
但是我还是乐意用zepto 或者jquery 这样的第三方类库。
目标很明确,就是用他三个方法:
$.get
$.get('/whatevs.html', function (response) {
$(document.body).append(response)
})
$.getJSON
$.getJSON('/awesome.json', function (data) {
console.log(data)
})
$.post
$.post('/create', {sample: 'payload'}, function (response) {
// process response
})
或者直接从一个表单中获取:
$.post('/create', $('#some_form').serialize(), function (response) {
// ...
})
如果response返回的消息体是一串字符的话,可以使用下面的方法 转化一下:
if (typeof response === 'string') {
response = JSON.parse(response)
}