Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
支持跨域JSON的WCF_Wcf_Json_Cross Domain - Fatal编程技术网

支持跨域JSON的WCF

支持跨域JSON的WCF,wcf,json,cross-domain,Wcf,Json,Cross Domain,我有一个用WebInvoke属性和WebHttp绑定修饰的WCF服务,用于启用JSON。在我们尝试使其跨域工作之前,可以从JavaScript访问该服务。你能推荐一下如何让这个跨域工作吗 我们尝试创建代理web处理程序,但每次WebHttpRequest尝试访问它时,它都会发出“错误请求”。我要做的是创建代理。跨域请求仅适用于GET动词,而不适用于POST。我所有的请求都通过代理,如果是帖子,那么它就充当典型的代理。如果请求使用GET,那么我必须将其转换为POST。 (我在服务合同中指定POST

我有一个用WebInvoke属性和WebHttp绑定修饰的WCF服务,用于启用JSON。在我们尝试使其跨域工作之前,可以从JavaScript访问该服务。你能推荐一下如何让这个跨域工作吗


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

我要做的是创建代理。跨域请求仅适用于GET动词,而不适用于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());
        }
    }

按照的第1-4部分中提供的步骤操作,最终得到一种干净的溶液。 我在生产中使用它没有任何问题

不过,您必须进行一次调整,才能使其适用于所有浏览器。在
CorsDispatchMessageInspector.BeforeSendReply
中注释掉检查:

如果(state.Message!=null)


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

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

创建一个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();
            }
        }
    }