0

I have to fetch data from my django api. In the url to fetch (http://127.0.0.1:8000/api/network/1/edges/) there are a number (pk integer) which can change. It can be http://127.0.0.1:8000/api/network/2/edges/ or http://127.0.0.1:8000/api/network/3/edges/. It depends on the current network. How can I handle this integer as variable ? I try this from (How to pass a variable with url on javascript fetch() method?) but it does not work

// Fetch data from api.
    var edges_from_api;
    var network_id = 1;
    fetch('http://127.0.0.1:8000/api/network/{network_id}/edges/')
        .then((response) => {
            return response.json()
        })
        .then((data) => edges_from_api=data)

1 Answer 1

4

use template literals

    var network_id = 1;
    fetch(`http://127.0.0.1:8000/api/network/${network_id}/edges/`)

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.