405 - Uncaught (in promise) Error: Request failed with status code 405

405 - Uncaught (in promise) Error: Request failed with status code 405

当我尝试从我的数据库中删除时出现此错误:

删除 https://localhost:44366/api/products/ 405

未捕获(承诺)错误:请求失败,状态代码为 405

我可以用 Postman 删除。我的获取和放置有效

我使用:

Azure 数据库

反应

ASP.NET核心API

MSSQL

反应: DbProvider.js:

   deleteProduct: async (id) => {
          let response = await axios.delete(this.state.baseApi + `/products/` + id)
          return response.data;
        }

更新 EditorProducts.js:

      import React, { useContext, useState, useEffect, useCallback } from "react";
import { Link } from "react-router-dom";

//confirm box
import confirm from "reactstrap-confirm";

// Icons
import { FaTrash } from "react-icons/fa";
import { FaEdit } from "react-icons/fa";
// Context
import DbContext from "../../../context/DbContext";

const EditorProducts = () => {
  const [products, setProducts] = useState();
  const [refresh, setRefresh] = useState(false);
  const context = useContext(DbContext);

  useEffect(() => {
    context.getProducts().then((x) => setProducts(x));
  }, [refresh]);

  const handleDelete = useCallback(async (event) => {
    event.persist();

    await confirm().then((response) => {
      console.log(event.target.id);
      console.log(response);

      if (response) {
    
        context.deleteProduct(event.target.id).then(() => setRefresh(true));
      }
    });
  });

  return (
    <>
      <table className="table">
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Title</th>
            <th scope="col">Image</th>
            <th scope="col">Actions</th>
          </tr>
        </thead>
        <tbody>
          {products != null ? 
            products.map(product => (
              <tr key={product.id}>
                  <th scope="row">{product.id}</th>
                  <td>{product.title}</td>
                  <td>{product.image}</td>
                  <td>
                    {/* EDIT PRODUCT */}
                    <Link to={`/editor/product/${product.id}`}>
                      <FaEdit style={iconStyle} />
                    </Link>
                    {/* DELETE PRODUCT */}
                    <a to="#" id={product.id} onClick={handleDelete}>
                      <FaTrash style={iconStyle} />
                    </a>
                  </td>
                </tr>
              ))
            : "Loading"}
        </tbody>
      </table>
    </>
  );
};

const iconStyle = {
  boxSizing: "unset",
  padding: "5px",
  PointerEvents: "none",
};

export default EditorProducts;

ProductsController.cs

   // DELETE: api/Products/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<Product>> DeleteProduct(int id)
        {
            var product = await _context.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            _context.Products.Remove(product);
            await _context.SaveChangesAsync();

            return product;
        }

        private bool ProductExists(int id)
        {
            return _context.Products.Any(e => e.Id == id);
        }

该问题似乎与 重复。 这是 Raphael Rafatpanah

接受的答案

The HTTP 405 error means that the server does not allow the HTTP request method that the client sent. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405 The HTTP method you're using in the code example you shared is POST. Therefore, it seems that your server does not accept POST requests.

为了解决这个问题,请将请求方法更改为受支持的方法,或者更改服务器以允许 POST 请求。

只需更改到此的路线并删除 return 产品,因为它已经被删除了

[Route("{id}")]
public async Task<ActionResult> DeleteProduct(int id)
  var product = await _context.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            _context.Products.Remove(product);
           var result = await _context.SaveChangesAsync();

           if (result ==0) return BadRequest();
           return Ok();
        }

和javascript

 let response = await axios.delete(this.state.baseApi + `/products/deleteproduct/` + id)