Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

how to call api with id vue js

i'm trying to call my api url/api/notification/{{id}} in vue js. My browser URL is already http://192.168.0.130:8080/notification/6. Problem is, I'm not getting my id(consol error).

What can I do?

0 likes
15 replies
tomasosho's avatar
Vue.axios.get('url/api/notification/${notice.id}')

This is not working also

tykus's avatar

If you want to use string interpolation, you need to use backticks:

Vue.axios.get(`url/api/notification/${notice.id}`)

Is the URL correct?

tomasosho's avatar

@tykus my router

{
    path: "/notification/:id",
    name: "Chat",
    meta: {
      auth: true
    },
    component: Chat
  },

Current URL

http://192.168.0.130:8080/notification/6

the console error

url/api/%60/api/notification/$%7Bid%7D%60

still doesn't fetch the id

tykus's avatar

@tomasosho this is URL encoding of

url/api/`/api/notification/${id}`

So wherever you are making the XHR request; the URL is screwed up. Are you running npm run dev whenever you are changing the JS source?

tomasosho's avatar

@tykus :to="`/notification/${item.id}`" then to router

{
    path: "/notification/:id",
    name: "Chat",
    meta: {
      auth: true
    },
    component: Chat
  },
tykus's avatar

@tomasosho but the console error is still url/api/%60/api/notification/$%7Bid%7D%60???? The code you're sharing and the error message are not making sense...

tomasosho's avatar

@tykus so i fixed the link but I'm getting /api/notification/undefined 500 (Internal Server Error)

Vue.axios.get(`/notification/`+this.id)
tomasosho's avatar

This works

Vue.axios.get(`/notification/`+this.$route.params.id)
EdwardCandisMaTeresa's avatar

I have one action which makes a call to API on success I call a mutation with data. Vue Component this.$store.dispatch('AxiosAction'',{ action : 'getuser' , id : 1} }

state :{ axiosActions : { 'getuser' :{ url : '/getuser' }

}

}

Vuex Action AxiosAction(data) { url = mainurl + state[data.action].url axios() then() { this.commit(data.action , response) } }

Vuex Mutation getuser(data) { state.user = data }

Bases on the action, https://echat.date i get the URL for the Endpoint

Please or to participate in this conversation.