将 URL id 发布到 Flask 蓝图端点

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

我正在使用带有蓝图的烧瓶。我试图通过 post 请求从 url 传递 ID,但我不明白这里的行为:

我在 localhost/2/updatestrat 页面上

这将发布到/add_indicator

let indi_data = await postJsonGetData(data, "/add_indicator");
async function postJsonGetData(data, endpoint, method = "POST") {
  const options = {
    method: method,
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  };
  let response = await fetch(endpoint, options);

  if (!response.ok) {
    throw new Error("Request failed");
  }

  const responseData = await response.json();
  return responseData;
}

这将发布到/2/load_conditions

  const { sell_conds, buy_conds } = await getJson("load_conditions");
async function getJson(endpoint) {
  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  let response = await fetch(endpoint, options);

  if (!response.ok) {
    throw new Error("Request failed");
  }

  const responseData = await response.json();
  console.log(responseData, "DDDDDDDD");
  return responseData;
}

为什么 getJson 会在端点中包含页面的 id,而 postJsonGetData 却不会?

我可以像这样轻松地从 getJson 获取数据

@bp.route('/<int:id>/load_conditions', methods=['POST'])
def load_conditions(id):

但是当我尝试对“/add_indicator”做同样的事情时

@bp.route('/<int:strategy_id>/add_indicator', methods=('POST',))
@login_required
def add_indicator(strategy_id):
    if request.method == 'POST':
        print(strategy_id)

我收到此错误: 发布 http://127.0.0.1:5000/add_indicator 404(未找到)

很明显它没有发布到正确的端点。

javascript python flask post
1个回答
0
投票

我找到了解决方案。 而不是:

let indi_data = await postJsonGetData(data, "/add_indicator");

我应该做:

let indi_data = await postJsonGetData(data, "add_indicator");
© www.soinside.com 2019 - 2024. All rights reserved.