最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data by id using Axios and VueJS - Stack Overflow

programmeradmin7浏览0评论

I have this get-function on my server-side that I want to use to get a specific entry in my database by giving the ID as a parameter. In my VueJS application I've written an Axios get-call but it doesn't give me any data. It only says

"Cannot GET ...".

I know the ID I'm giving the function is correct so that's not the problem.

Client-side:

loadData() {
     axios.get('http://localhost:8080/route', {
        params: {
            id: this.selectedRoute
        }
     })
     .then(response => (this.chosenRoute = response.data));
}

Server-side:

app.get('/route/:id', function(req, res) {
    const routeId = req.params.id;
    Route.findById(routeId, (err, route) => {
        res.set("Access-Control-Allow-Origin", "*");
        res.json(route);
    });
});
    enter code here

I have this get-function on my server-side that I want to use to get a specific entry in my database by giving the ID as a parameter. In my VueJS application I've written an Axios get-call but it doesn't give me any data. It only says

"Cannot GET ...".

I know the ID I'm giving the function is correct so that's not the problem.

Client-side:

loadData() {
     axios.get('http://localhost:8080/route', {
        params: {
            id: this.selectedRoute
        }
     })
     .then(response => (this.chosenRoute = response.data));
}

Server-side:

app.get('/route/:id', function(req, res) {
    const routeId = req.params.id;
    Route.findById(routeId, (err, route) => {
        res.set("Access-Control-Allow-Origin", "*");
        res.json(route);
    });
});
    enter code here
Share Improve this question edited Nov 15, 2018 at 21:54 Boussadjra Brahim 1 asked Nov 15, 2018 at 21:45 M. HM. H 3393 gold badges7 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Try something like this by concatenating the id which is this.selectedRoute to the url :

  loadData() {
     axios.get('http://localhost:8080/route/'+ this.selectedRoute)
              .then(response => (this.chosenRoute = response.data));
}

In your last question you have this :

      <select name="routeSelect" v-model="selectedRoute">
          <option v-for="routes in result" v-bind:key="routes.name" v- 
           bind:value="routes.name"> {{ routes.name }} </option>
      </select>

this doesn't work because the selectedRoute is the route name which isn't not an id to solve that you have to do this :

          <option v-for="routes in result" v-bind:key="routes.name" v- 
           bind:value="routes.id" >...

by binding the select options value to the route id

发布评论

评论列表(0)

  1. 暂无评论