Jquery

1-1 get

  1. $.get(url,res=>{
  2. console.log(res)
  3. })
  4. $.get(url).then(res=>{
  5. console.log(res)
  6. })

1-2 $.ajax

  1. $.ajax({
  2. url,
  3. type:"get",
  4. data, // get传值问号后面的值,可以放在data属性里面
  5. dataType,
  6. success:res=>{
  7. console.log(res);
  8. }
  9. })
  1. var url ="http://192.168.4.18:3000/search"
  2. $.ajax({
  3. url,
  4. type:"get",
  5. data:{
  6. keywords:"你"
  7. },
  8. success:res=>{
  9. console.log(res);
  10. }
  11. })

Axios

  1. Axios 是一个基于 promise HTTP 库,可以用在浏览器和 node.js 中。
  1. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2-1 get

  1. axios.get(url).then(res=>{
  2. console.log(res);
  3. })
  1. // var url = "http://192.168.4.18:3000/search?keywords=你"
  2. axios.get("http://192.168.4.18:3000/search",{
  3. params:{
  4. keywords:"你"
  5. }
  6. }).then(res=>{
  7. console.log(res);
  8. })

2-2 axios

  1. axios({
  2. url,
  3. baseURL,
  4. method,
  5. params:{} //问号后面的值
  6. }).then(res=>{
  7. console.log(res)
  8. })
  1. axios({
  2. url:"/search",
  3. baseURL:"http://192.168.4.18:3000",
  4. method:"get",
  5. params:{
  6. keywords:"你"
  7. }
  8. }).then(res=>{
  9. console.log(res);
  10. })

2-3 axios二次封装

  1. var baseURL="http://192.168.4.18:3000/"
  2. function http({
  3. url,
  4. method:"get",
  5. params:{}
  6. }){
  7. return axios({
  8. url,
  9. baseURL,
  10. method,
  11. params
  12. })
  13. }
  1. http({
  2. url:"/album?id=96964667",
  3. }).then(res=>{
  4. console.log(res)
  5. })
  6. http({
  7. url:"/album",
  8. params:{
  9. id:"96964667"
  10. }
  11. }).then(res=>{
  12. console.log(res)
  13. })