代码之家  ›  专栏  ›  技术社区  ›  user23117

跨域启用json的wcf

  •  0
  • user23117  · 技术社区  · 16 年前

    我有一个用webinvoke属性和webhttp绑定修饰的wcf服务,用于json启用。在我们尝试使服务跨域工作之前,可以从javascript访问该服务。你能推荐如何让这个跨域工作吗?

    我们试图创建代理web处理程序,但每次webhttprequest试图访问它时,它都会发出“错误请求”。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Mike_G    16 年前

    我要做的是创建一个代理。跨域请求只适用于get动词,而不适用于post。我的所有请求都通过代理,如果是post,那么它就充当典型的代理。如果请求使用get,那么我必须将其转换为post。 (我在服务合同中指定post作为动词)。

    在客户端,我使用jquery的josnp(带填充的json)功能将正确的信息附加到querystring。

    private static readonly Properties.Settings settings = new Properties.Settings();
    
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string wcfAddress = context.Request.QueryString["WcfAddress"];                       
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(settings.WCFAddress + wcfAddress);
    
                request.ContentType = "application/json";
    
                request.Method = "POST";
    
                if (context.Request.RequestType == "GET")
                {
                    string callback = context.Request.QueryString["callback"];
                    string qs = context.Request.QueryString[null];
                    byte[] body = body = Encoding.UTF8.GetBytes(qs);
    
                    request.ContentLength = body.Length;
    
                    request.GetRequestStream().Write(body, 0, body.Length);
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
    
                        string contents = reader.ReadToEnd();
    
                        contents = callback + "(" + contents + ");";
    
                        context.Response.ContentType = "application/json";
    
                        context.Response.Write(contents);
    
                        response.Close();
    
                        reader.Close();
                    }
                }
                else if (context.Request.RequestType == "POST")
                {
                    byte[] body = new byte[context.Request.ContentLength];
    
                    context.Request.InputStream.Read(body, 0, body.Length);
    
                    request.ContentLength = body.Length;
    
                    request.GetRequestStream().Write(body, 0, body.Length);
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        string contents = reader.ReadToEnd();
    
                        context.Response.ContentType = "application/json";
    
                        context.Response.Write(contents);
    
                        response.Close();
    
                        reader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
    
        2
  •  0
  •   ann user874538    10 年前

    遵循中提供的步骤 第1-4部分 属于 this excellent article series 最后你得到了一个干净的解决方案。 我在生产中使用它没有任何问题。

    你必须做一个调整,使它与所有浏览器工作。在 CorsDispatchMessageInspector.BeforeSendReply 签出支票:

    如果(状态信息!=空)

    否则,“允许”标题只应用于飞行前请求,而不应用于实际请求。

        3
  •  0
  •   Marcelo Rodrigues    8 年前

    要解决问题,请执行以下操作

    创建一个global.asax并添加以下代码以启用ajax跨域post

     public void Application_BeginRequest(object sender, EventArgs e)
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST,OPTIONS");
    
                if ((HttpContext.Current.Request.HttpMethod == "OPTIONS"))
                {
    
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                    HttpContext.Current.Response.End();
                }
            }
        }