有关如何执行http:// localhost:3000 / $ {id}的任何示例?

问题描述 投票:-1回答:1

[请给我一个有关如何使用此方法的示例,因为我有点迷茫(Vanilla JS)

提前感谢。

javascript axios id
1个回答
0
投票

获取API

var id = 3000;

fetch('http://localhost:3000/' + id)
  .then(res => res.json())
  .then(function (data) {
    // handle success
    console.log(data);
  });

Axios

var id = 3000;

axios.get('http://localhost:3000/' + id)
  .then(function (response) {
    // handle success
    console.log(response.data);
  });
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

PS:您也可以使用http://localhost:3000/${id}作为网址字符串。需要使用tilda(`)代替单(')或双(“)字符串!


0
投票

只需以这种方式在url中提供ID,以便发布或放置,您可以将正文作为第二个参数发送。

axios.get('http://localhost:3000/'+id).then(response=>{
console.log(response.data)}).catch(err=>console.log(err);
© www.soinside.com 2019 - 2024. All rights reserved.