【问题标题】:React DELETE method not allowed不允许反应 DELETE 方法
【发布时间】:2020-07-08 19:47:29
【问题描述】:

我有一个 dotnet core 3.1 API 和一个 React 应用程序。

当我使用 React 应用程序执行 DELETE 请求时,我得到了一个不允许的方法 (405)。

DelBiere.tsx:45 DELETE https://localhost:44339/api/v1/biere 405

还有

4:1 Uncaught (in promise) SyntaxError: Unexpected token S in JSON at position 0.

但是当我使用 Postman 时,一切正常。

此外,GET 和 POST 请求适用于 React 应用。

我没有在 IIS 上安装 WEBDAV。

我有一个这样的 web.config 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="AspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" requireAccess="Script" />
    </handlers>

    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

我的反应处理函数如下所示:

handleSubmit(event: any) {
  fetch('https://localhost:44339/api/v1/biere', {
    method: 'DELETE',
    headers : {      
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body:JSON.stringify(this.id)
  })
    .then((res) => console.log(res.json(), "res"))
    .then((data) =>  console.log(data, "data"))
    .catch((err)=>console.log(err, "err"));
  event.preventDefault();
}

感谢您的帮助!

【问题讨论】:

    标签: reactjs asp.net-core iis


    【解决方案1】:

    问题是我在fetch 函数的body 属性中添加了this.id,如下所示:body:JSON.stringify(this.id)

    但我的 API 端点需要 URL 中的 id。

    [HttpDelete("{id:int}")]
    public async Task<IActionResult> Delete(int id)
    {
        //Some code
    }
    

    这就是我解决问题的方法。

    handleSubmit(event: any) {
        const url = 'https://localhost:44339/api/v1/biere/' + this.id;
    
        const options = {
            method: 'DELETE',
            headers : {      
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        }
    
        fetch(url, options)
            .then((res) => console.log(res.json(), "res"))
            .then((data) => console.log(data, "data"))
            .catch((err) => console.log(err, "err"));
    
        event.preventDefault();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2023-02-17
      • 2018-11-12
      • 2019-10-28
      • 2020-09-15
      • 2017-09-03
      • 1970-01-01
      相关资源
      最近更新 更多