【问题标题】:CORS implementation showing method not allowed for POST requestCORS 实现显示不允许用于 POST 请求的方法
【发布时间】:2019-10-13 01:02:28
【问题描述】:

我正在尝试使用来自另一个项目的 Http POST 请求调用 WCF 服务方法,它允许 GET 请求,但是当我尝试 POST 请求时,它显示 “不允许方法”

当我尝试时,它在我的控制台中显示此错误:

从源“http://localhost:6538”访问“http://localhost:7280/api/values/24”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。

网络 API: 方法:

[WebInvoke(UriTemplate = "/values", Method = "POST", ResponseFormat = WebMessageFormat.Json), CorsEnabled]
        void AddValue(string value);

方法实现

public void AddValue(string value)
        {
            string id = nextId.ToString();
            nextId++;
            allValues.Add(id, value);
            string location = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString() + "/" + id;
            WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(new Uri(location));
        }

客户端应用程序:

$("#post").click(function () {
            var data = "\"" + $("#value").val() + "\"";
            $.ajax({
                url: valuesAddress,
                type: "POST",
                contentType: "application/json",
                data: data,
                success: function (result) {
                    $("#result").text(result);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $("#result").text(textStatus);
                }
            });
        });

我关注了 https://code.msdn.microsoft.com/Implementing-CORS-support-c1f9cd4b/view/SourceCode#content 我将 Web API 和客户端应用程序创建为不同的项目。 请帮忙解决这个问题。

【问题讨论】:

标签: c# asp.net angularjs wcf .net-framework-version


【解决方案1】:

您已启用 Cors 属性,当您查看 documentation MSDN 提供时,您会发现您必须在类或方法上方提供该属性。 下面的代码允许类中的所有方法都可以跨域调用。

[EnableCors(origins: "*", headers: "*", methods: "*")]
public class Service1 : IService1
{
  // Details omitted
}

下面的代码为 GetAllStudents 方法启用了 cors。

public class Service1 : IService1
{
  [EnableCors(origins: "*", headers: "*", methods: "*")]
  List<string> GetAllStudents()
  {
    // Details omitted
  }
}

【讨论】:

  • 仍然无法正常工作。我一步一步地按照文档进行操作,但没有奏效。文档中的示例在一个项目中包含 HTML 和服务,我的问题是我将 Web 层和服务层分开。
【解决方案2】:

我自己在使用 CORS 时遇到了问题,这是我在 asp.net 中的做法(根据您的标签判断相同):

  • 我已经安装了以下 NuGet 包:

    安装包 Microsoft.AspNet.WebApi.Cors

  • 然后在您的 WebApiConfig 文件中:
        public static void Register(HttpConfiguration config)
        {
            EnableCorsAttribute cors = new 
                 EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);

            AddRoutes(config);
        }

这一步你已经完成了,只需将EnableCors(cors)方法移到AddRoutes(config);上方即可

  • 最后,您将以下属性放在 SaveWithPost 上方:
[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]

注意:如果您不断收到此错误:

选项http://localhost/Service1.svc/saveWithPost/WithParameter 405(不允许的方法) 从源“http://localhost”访问“http://localhost/Service1.svc/saveWithPost/WithParameter”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。

阅读此处the official documentation of Microsoft。该链接将您带到 Cors 的工作方式下,您可以在其中阅读有关预检请求的更多信息。很快,浏览器会在实际请求之前发送一个附加请求。 阅读更多here

另一个注意事项:检查您是否没有阻止 OPTIONS 请求。例如,一些公司使用基于网络设备和云服务的安全、网络和存储产品的软件,过滤所有即将到来的对他们自己服务器的请求,这有时可能会导致这种情况。 (不太可能)

希望这会有所帮助,

干杯

【讨论】:

  • 我试过这个但得到同样的错误“方法不允许”。
猜你喜欢
  • 2023-03-10
  • 2019-01-04
  • 1970-01-01
  • 2020-11-02
  • 2017-12-09
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2016-04-23
相关资源
最近更新 更多