axios数据请求方式

Love The Way You Lie 2022-05-09 06:10 316阅读 0赞

70

70 1

70 2

get请求

  1. get:function () {
  2. axios.get("../package1.json",{
  3. params:{
  4. userId:"999"
  5. },
  6. headers:{
  7. token:"jack"
  8. }
  9. }).then(res=>{
  10. this.msg=res.data;
  11. }).catch(function (error) {
  12. console.log("error init." +error)
  13. });
  14. },

post请求

  1. post:function () {
  2. axios.post("../package.json",{
  3. userId:"888"
  4. },{
  5. headers:{
  6. token:"tom"
  7. }
  8. }).then(res=>{
  9. this.msg = res.data;
  10. }).catch(function (error) {
  11. })
  12. },

http请求

  1. http:function () {
  2. axios({
  3. url:"../package.json",
  4. // method:"get",
  5. method:"post",
  6. data:{
  7. userId:"101"
  8. },
  9. // params:{
  10. // userId:"102"
  11. // },
  12. headers:{
  13. token:"http-test"
  14. }
  15. }).then(res=>{
  16. this.msg = res.data;
  17. })
  18. }

在http请求里,如果methods是post方式的话,就要用 data:{ userId:”101” },传入参数,如果是get请求的话,用params

axios全局拦截器的使用

  1. mounted:function(){
  2. axios.interceptors.request.use(function (config) {
  3. console.log("request init.");//在响应之前打印,也就可以在里面写一些请求之前要做的事
  4. return config;
  5. })
  6. axios.interceptors.response.use(function (response) {
  7. console.log("response init.");//在响应之后打印,也就可以在里面写一些请求之后要做的事
  8. return response;
  9. })
  10. },

发表评论

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

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

相关阅读

    相关 axios请求数据

    自从用饿了么框架重构项目以来,遇到 很多问题,我都有一一记录下来,现在特喜欢这个框架,说实话,如果你是用了vue这个技术栈的话,一定要用饿了么的pc端框架哦,遇到问题的时候在网