axios 的请求方式

迈不过友情╰ 2022-10-29 01:46 251阅读 0赞
  1. // 使用默认进行请求(默认是get)
  2. axios({
  3. url: "http://localhost:9999/student/student/getAllStudent"
  4. }).then(res => {
  5. console.log(res);
  6. })
  7. // 指定请求方式为get无参请求
  8. axios({
  9. url: "http://localhost:9999/student/student/getAllStudent",
  10. method: "get"
  11. }).then(res => {
  12. console.log(res);
  13. })
  14. // 指定请求方式为get有参请求(方式一: 参数直接拼接到url)
  15. axios({
  16. url: "http://localhost:9999/student/student/getAllStudent?id=1",
  17. method: "get",
  18. }).then(res => {
  19. console.log(res);
  20. })
  21. // 指定请求方式为get有参请求(方式二:参数放到params)
  22. axios({
  23. url: "http://localhost:9999/student/student/getAllStudent",
  24. method: "get",
  25. params: {
  26. id: 1
  27. }
  28. }).then(res => {
  29. console.log(res);
  30. })
  31. // axios post请求, 携带参数时content-type默认是application-json;
  32. // 指定请求方式为post的无参请求
  33. axios({
  34. url: "http://localhost:9999/student/student/getAllStudent",
  35. method: "post"
  36. }).then(res => {
  37. console.log(res);
  38. })
  39. // 指定请求方式为post的有参请求,使用params传递参数
  40. axios({
  41. url: "http://localhost:9999/student/student/getAllStudent",
  42. method: "post",
  43. params: {
  44. id: 1,
  45. name: '张三'
  46. }
  47. }).then(res => {
  48. console.log(res);
  49. })
  50. // 指定请求方式为post的有参请求,使用data传递参数;
  51. axios({
  52. url: "http://localhost:9999/student/student/getAllStudent",
  53. method: "post",
  54. data: {
  55. id: 1,
  56. name: '张三'
  57. }
  58. }).then(res => {
  59. console.log(res);
  60. })

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读