# jquery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| $.ajax({ url:"demo_test.txt", success:function(result){ console.log(result); } }
$.ajax({ url:"demo_test.txt", method:"POST", success:function(result){ console.log(result); } }
$.ajax({ url:"demo_test.txt", headers:{a:"aaa"}, data:JSON.stringify({a:10}), success:function(result){ console.log(result); }, error:function(xhr,status,error){ console.log(error); } });
$.ajax({ url:"demo_test.txt", headers:{ contentType: "application/json"}, method:"POST", data:JSON.stringify({a:10}), success:function(result){ console.log(result); } });
|
# fetch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| fetch(url, { headers: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'POST', body: 'a=12&b=33' }) .then(res => res.json()) .then(data => console.log(data)) .catch(e => {})
fetch(url, { headers: { 'content-type': 'application/json' }, method: 'POST', body: JSON.stringify({ a: 100 }) }) .then(res => res.json()) .then(data => console.log(data)) .catch(e => {})
|
# axios
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| axios({ url: 'http://localhost:99?x=1', method: 'POST', data: { a: 12 } }).then(res => console.log(res.data))
axios({ url: 'http://localhost:99?x=1', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: 'a=12&b=23' }).then(res => console.log(res.data))
|
# 简写
JQuery 的 get 和 post 可以简写:
1 2
| $.get(url, data, callback) $.post(url, data, callback)
|
axios 的 get/post/put/delete 等等都可以简写
1
| axios.post(url, data).then(callback)
|
引用: https://www.bilibili.com/video/av61002444