记录HttpListener 在C#winform中跨域问题

 private void getContextCallback(IAsyncResult ar)
        {
            HttpListener server = (HttpListener)ar.AsyncState;
            HttpListenerContext ctx = server.EndGetContext(ar);
            this.httpserver.BeginGetContext(new AsyncCallback(getContextCallback), this.httpserver);
            ctx.Response.StatusCode = 200;
            ctx.Response.ContentType = "application/json";
            ctx.Response.AppendHeader("Access-Control-Allow-Origin", "*");

            string action = ctx.Request.QueryString["action"];

            switch (action)
            {
                case "finger":
                    getFinger(ctx);
                    break;
                case "idcard":
                    getIDCard(ctx);
                    break;
                case "signature":
                    getSignature(ctx);
                    break;
            }

}

最重要的一句就是:  ctx.Response.AppendHeader("Access-Control-Allow-Origin", "*");

添加这句就好使了

猜你喜欢

转载自blog.csdn.net/weixin_41043580/article/details/79744687