本文介绍了选项405(方法不允许),无论服务器发送Access-Control-Allow-Methods:OPTIONS,GET,HEAD,POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行跨域请求,我的服务器配置为发送以下标头:

I'm trying to make cross-domain request and my server is configured to send the following headers:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with, Authorization
Access-Control-Allow-Methods:OPTIONS, GET, HEAD, POST
Access-Control-Allow-Origin:*

但是当发出OPTION请求时,我得到 OPTIONS 405(方法不允许)错误。

But when an OPTION request is made, I get OPTIONS 405 (Method Not Allowed) error.

任何想法有什么问题以及如何解决?

Any Ideas what is the problem and how to fix it?

推荐答案

我建议2个解决方案:

1)如果您使用的是WebAPI,那么需要来实现按惯例应该看起来像的方法:

1) If you are using WebAPI you need to implement the option method that by convention should look like:

public class XXXController : ApiController
{
    // OPTION http-verb handler
    public string OptionsXXX()
    {
        return null; // HTTP 200 response with empty body
    }

    ...
}

2)如果您使用WebAPI尝试了解代码的哪一部分触发选项405(方法不允许)错误。在这种情况下,我会检查是否尝试添加到 Web.config 文件这些< customHeaders /> 有效:

2) If you are not using WebAPI try to understand which part of your code triggers the OPTIONS 405 (Method Not Allowed) error for the OPTION call. In that case I would check if trying to add to the Web.config file these <customHeaders/> that works:

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <!-- CORS temporary solution -->
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Authorization, Accept, X-Requested-With" />
        <add name="Access-Control-Allow-Methods" value="OPTIONS, TRACE, GET, HEAD, POST, PUT" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

这篇关于选项405(方法不允许),无论服务器发送Access-Control-Allow-Methods:OPTIONS,GET,HEAD,POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 13:28