首页 > 解决方案 > 尝试删除 id 数组时,删除请求 api 在控制台中显示错误

问题描述

我试图通过实现一个带有查询参数的处理程序来删除一个 id 数组。我之前实现了一个删除 api 函数,它接受一个 id 并将其删除。但是,现在我创建了一个新端点,它将清除整个数组。新端点是:

上面端点中的 id 只是虚拟的,用于向您展示它应该如何获取 id。

configIds 作为查询参数传递,响应将是这样的:

所以我所做的是将新端点添加到现有的删除 api 函数中,该函数当前需要一个 configId。所以:

async function deleteConfig(configId) {
  const options = {
    method: 'DELETE',
    headers: {
      'content-type': 'application/json'
    },
    url: configId.includes(',') ? `/api/v1/algo/configs?configIds=${configId}` : `${ALGOS_API_ROOT}/configs/${configId}`
  };
  return axios(options);

但是,我在这里遇到错误:

api/v1/private/api/v1/algo/configs?configIds=41,40,38,23,22 404 (Not Found)

如您所见,正在传递 id。但是有一个错误。任何帮助,将不胜感激。谢谢!

标签: javascriptreactjsapiasynchronousreact-redux

解决方案


在请求正文中发送 id 数组。

async function deleteConfig(configId) {
  const options = {
    method: 'DELETE',
    headers: {
      'content-type': 'application/json'
    },
    url: `${ALGOS_API_ROOT}/configs`,
    data: {
      ids: [1, 2, 3]
    }
  };
  return axios(options);
}

推荐阅读