如何在 Fetch GET 请求中插入参数?
How to insert a params in Fetch GET request?
我创建了一个简单的 Flask 的 jsonify,我不确定是否可以将其称为 API,但我收到一个 JSON 对象。
@app.route('/searchData/<int:id>',methods=["GET"])
def searchData(id):
return jsonify(searchData(id))
现在使用 Fetch 函数,我无法传递参数 <int:id>
,如果我可以将参数添加到 Flask 中的 Header 以接受参数,我不会。
async fetch(id){
const res = await fetch(
"http://localhost:5000/searchData/",
{
method:"GET"
}
);
const data = await res.json()
console.log(data)
}
将 "
更改为反引号,使其成为字符串文字并使用插值添加。
async fetch(id){
const res = await fetch(
`http://localhost:5000/searchData/${id}`,
{
method:"GET"
}
);
const data = await res.json()
console.log(data)
}
使用这种插值方式可以让您在有几个参数时更容易阅读
我创建了一个简单的 Flask 的 jsonify,我不确定是否可以将其称为 API,但我收到一个 JSON 对象。
@app.route('/searchData/<int:id>',methods=["GET"])
def searchData(id):
return jsonify(searchData(id))
现在使用 Fetch 函数,我无法传递参数 <int:id>
,如果我可以将参数添加到 Flask 中的 Header 以接受参数,我不会。
async fetch(id){
const res = await fetch(
"http://localhost:5000/searchData/",
{
method:"GET"
}
);
const data = await res.json()
console.log(data)
}
将 "
更改为反引号,使其成为字符串文字并使用插值添加。
async fetch(id){
const res = await fetch(
`http://localhost:5000/searchData/${id}`,
{
method:"GET"
}
);
const data = await res.json()
console.log(data)
}
使用这种插值方式可以让您在有几个参数时更容易阅读