【问题标题】:How to insert a params in Fetch GET request?如何在 Fetch GET 请求中插入参数?
【发布时间】:2020-06-30 22:39:53
【问题描述】:

我创建了一个简单的 Flask 的 jsonify,我不确定是否可以将其称为 API,但我收到了一个 JSON 对象。

@app.route('/searchData/<int:id>',methods=["GET"])
def searchData(id):
    return jsonify(searchData(id))

现在使用 Fetch 函数,我无法传递 arg &lt;int:id&gt;,如果我可以在 Flask 中将参数添加到 Header 以接受参数,我不会。

  async fetch(id){      
    const res = await fetch(
      "http://localhost:5000/searchData/",
        {
          method:"GET"
        }
    );
    const data = await res.json()
    console.log(data)
  }

【问题讨论】:

  • 在searchData/后输入!
  • 对不起,你能解释一下吗?

标签: javascript vue.js flask restful-url


【解决方案1】:

" 更改为反引号,使其成为字符串文字并通过插值添加。

async fetch(id){      
    const res = await fetch(
      `http://localhost:5000/searchData/${id}`,
        {
          method:"GET"
        }
    );
    const data = await res.json()
    console.log(data)
  }

当你有几个参数时,使用这种插值风格会更容易阅读

【讨论】:

  • 感谢您的回答,我在 VUE.JS 中收到错误提示 error 'id' is defined but never used
  • 是的,那是在谈论您创建的 fetch(id) 方法。在 URL 中使用这种用法就足够了 Vue 并清除该错误
  • 我按照你解释的方式做了,但是错误提示async fetch(id){ 从未使用过
  • 这可能不起作用,但请尝试将 async fetch(id){ 名称更改为 fetchDataById(id) 或其他任何您喜欢的外观。只是好奇它的阅读本身是否错误,因为如果名称相同
  • 我必须修复它“连接” url + id 如'http://localhost:5000/searchData/'+id 谢谢你帮助我@Floyd
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2015-11-14
  • 2020-08-20
相关资源
最近更新 更多