文章标题

C#的ajax跨域探讨

在百度上,看到方法很多,我参考了一种,比较好的解决方案。

直接在服务器中的一般处理程序中,添加http头,

context.Response.ContentType = "text/plain";

            //context.Response.AppendHeader("Access-Control-Allow-Origin", "http://localhost:64685");
            context.Response.AppendHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8020");
            context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
            context.Response.AppendHeader("Access-Control-Allow-Methods","OPTIONS,POST,GET"); 
            context.Response.AppendHeader("Access-Control-Allow-Headers", "Session,Content-Range,Content-Disposition,content-type");

然后,在前端页面,如果要 获取session的值,还需要在ajax的参数中加上

xhrFields: { withCredentials: true },
crossDomain: true,

你可能感兴趣的:(跨域,Ajax,C#)