Gah, so this is a CORS problem.
I automatically add CORS headers to the response:
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) { pipelines.AfterRequest.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"); }); pipelines.OnError.AddItemToEndOfPipeline((ctx, exc) => { if (exc != null) { throw exc; } return HttpStatusCode.InternalServerError; }); base.RequestStartup(container, pipelines, context); }
But when the response is replaced in my status code handler, I need to set these headers again:
public class JsonErrorStatusCodeHandler : IStatusCodeHandler { public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) { if (statusCode != HttpStatusCode.InternalServerError) { return false; } var exception = context.GetException(); return exception != null; } public void Handle(HttpStatusCode statusCode, NancyContext context) { var exception = context.GetException(); JsonResponse response = new JsonResponse(string.Format("{0}:{1}", exception, exception.Message), new DefaultJsonSerializer()); response.StatusCode = HttpStatusCode.InternalServerError; context.Response = response; context.Response.WithHeader("Access-Control-Allow-Origin", "*") .WithHeader("Access-Control-Allow-Methods", "POST,GET") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"); } }
Ehren source share