让 CORS 与 Nancy 合作 [英] Getting CORS To Work With Nancy

查看:28
本文介绍了让 CORS 与 Nancy 合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让所有类型的请求与 Nancy 和 CORS 一起使用.目前我在请求的末尾添加了一个管道:

I am trying to get all types of requests to work with Nancy and CORS. Currently I add a pipeline at the end of the request:

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => ctx.Response
            .WithHeader("Access-Control-Allow-Origin", "http://localhost:57515")
            .WithHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS")
            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type")
            .WithHeader("Allow", "POST, GET, DELETE, PUT, OPTIONS"))

选项请求返回的状态代码为 200,这让我相信它执行得很好,但对于除 OPTIONS 以外的任何类型的请求,它都会失败并显示 405 Method Not Allowed.为了让它工作,我还需要在客户端或服务器端做些什么吗?

The options request comes back with a status code of 200, which leads me to believe that it executed fine, but for any type of request other than OPTIONS it fails with 405 Method Not Allowed. Is there anything else that I need to do either client side or server side in order to get this to work?

我使用的客户端库是主干.

The client side library I am using is backbone.

提前致谢.

推荐答案

我认为您不需要将 OPTIONS 指定为允许的 CORS 方法.我从来没有见过那一套,我也从来没有自己设置过.浏览器不会抱怨.

I don't think you need to specify OPTIONS as an allowed CORS method. I've never seen that set, and I've never set it myself. Browsers don't complain.

另外,我的设置与您类似:

Otherside I have a similar setup as you in my :

public abstract class MyModule : NancyModule
    protected MyModule(bool hasRazorView = true)
        After.AddItemToEndOfPipeline((ctx) => ctx.Response
            .WithHeader("Access-Control-Allow-Origin", "*")
            .WithHeader("Access-Control-Allow-Methods", "POST,GET")
            .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));
        . . . .
    }
}

就我而言,CORS 在我的 GET 和 POST 上发送,但不是 OPTIONS.我用 Options["/"] = route => 覆盖了默认的 OPTIONS 处理.新的响应().这让管道的其余部分着火了.

In my case, the CORS get sent on my GET and POST, but not the OPTIONS. I overrode the default OPTIONS handling with Options["/"] = route => new Response(). That let the rest of the pipeline fire.

这篇关于让 CORS 与 Nancy 合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆