Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CORS To Work With Nancy

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"))

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.

Thanks in advance.

like image 998
The Pax Bisonica Avatar asked Oct 03 '14 00:10

The Pax Bisonica


1 Answers

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"));
        . . . .
    }
}

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.

like image 85
Justin Dearing Avatar answered Oct 10 '22 06:10

Justin Dearing