You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Axios发送DELETE请求遇405错误,POSTMAN测试正常的解决问询

Fixing Axios DELETE Request for Path Parameters

Hey there! Let's break down why your Axios DELETE request is failing and how to fix it.

The Core Issue

Looking at your Axios code, the main problem is how you're constructing the request URL. You're using '/api/flavour/{id}' with single quotes—this treats {id} as a literal string instead of replacing it with the actual id value passed into the function. That means your request is being sent to localhost:5000/api/flavour/{id} instead of the correct URL with your target document ID, which explains the 404/405 errors you're seeing.

Correct Axios Implementation

You need to use template literals (backticks ` instead of single quotes) to dynamically inject the id into the URL path. Here's the fixed code:

DeleteFlavour: async (id) => { 
  console.log(id); 
  // Use template literals to insert the actual id into the URL
  let res = await axios.delete(`/api/flavour/${id}`); 
  console.log(res); 
  return res.data || []; 
}

Why Your Previous Attempts Didn't Work

  • When you tried passing the id in Axios' second parameter, you were adding it as a query parameter (?id=xxx), but your backend is set up to expect it as a path parameter (/:id). These are two distinct URL formats, so the backend couldn't match the route.
  • Hardcoding the URL likely failed because you either kept the placeholder {id} instead of using the actual ID value, or there was a subtle typo in the URL structure that didn't align with the backend route.

Additional Checks (If Issues Persist)

If you still run into problems after fixing the URL:

  • Verify your Axios instance has the correct baseURL set to localhost:5000 (so /api/flavour/${id} resolves to the full valid URL).
  • Double-check that there are no method mismatches: Axios uses .delete() and your backend uses app.delete()—method conflicts are a common cause of 405 errors.
  • Confirm the ID you're passing matches exactly with a valid document ID in your database (invalid IDs might return unexpected status codes even with a correct URL).

内容的提问来源于stack exchange,提问作者BC2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:37:36