【问题标题】:How to pass index from the li to axios?如何将索引从li传递到axios?
【发布时间】:2021-04-18 06:26:38
【问题描述】:

      selectPreviousSearch(index) {
        this.search = this.searchHistory.slice(-5).reverse()[index];
        // this.search = this.searchHistory[index];
        this.showSearchHistory = false;
        

        const baseURI = 'api url...';   

        var container_id =  JSON.parse(localStorage.getItem('anonymous_id'));
       
        let accToken = localStorage && localStorage.loggedinUser ? localStorage.loggedinUser : null;
          // eslint-disable-next-line
         console.log('another call',accToken);


        const headers = new Headers({
          'access_token' : accToken,
          'Content-Type': 'application/json',
        });

        var data={

          'keyword': this.index,

        } 

        axios.get(baseURI, data, {
          headers: {
            'access_token': accToken
          }
        })
        .then((response) => {
          this.users = response;
         
        }, (error) => {
          if (error) {
            this.errorMessage = error.response.data.message;
          }
        }).catch(error => {
          //this.errorMessage = error.response.data;
        })


      },
<li
            v-for="(item, index) in searchHistory
              .slice(-5)
              .reverse()
              .map((s) => s.trim())"
            :key="index"
            @click="selectPreviousSearch(index)"
          >
            {{ item }}
          </li>

我想将索引从 li 传递到 axios?在 var 数据中。这样当用户单击 li 中的数据时。他会得到成功响应。

目前我在想,我没有正确传递值,从 li 所以我无法看到正确的响应

【问题讨论】:

    标签: javascript vue.js axios


    【解决方案1】:

    尝试像这样传递获取参数

    axios.get(baseURI, {
       params: data,
       headers:...
    })
    

    文档https://github.com/axios/axios#example中的示例

    或者改变

    axios.get
    

    axios.post
    

    取决于您在服务器端处理的请求类型

    【讨论】:

      猜你喜欢
      • 2013-03-03
      • 1970-01-01
      • 2015-10-18
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多