简体   繁体   中英

Issue with Cross Domain Ajax call using Jquery WCF REST Starter Kit Preview 2

I have created and hosted my WCF REST API Using WCF REST Starter Kit preview2. Which supports Dynamic Response and Request format type (XML and JSON).Everything is working fine if I consume the service in same domain through Jquery, Microsoft.Http.

My cs code is as following:

private void GetData()
    {
        string url = string.Format("http://myhost/Services/UserService.svc/people/");
        HttpClient client = new HttpClient();
        HttpResponseMessage responseMessage = client.Get(url);
        responseMessage.EnsureStatusIsSuccessful();
        using (responseMessage)
        {
            string res = responseMessage.Content.ReadAsString();
            Response.Write(res);
        }
    }

now when I try to consume my service using Jquery from another domain Response is comming in IE8 but in Mozilla and Chrome I am getting null response My jquery code is as following :

 function loadData() {
    var path = "http://myhost/Services/UserService.svc/people/";       
    $.ajax({
        type: "GET",
        url: path,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(response) {               
            if (response != null) {
                displayData(response);                   
            }
        }
    });
}

I have also try to set Data type:"jsonp" and .getJson() method call.

You can also use a ProxyHandler on your current domain. From you client side code, call the proxy handler (same domain) which can then call the REST service (cross domain).

Check out these articles about creating a HtppHandler (Proxy) if you need a starting point.

How To Create an ASP.NET HTTP Handler by Using Visual C# .NET

A Boilerplate HttpHandler - Scott Hanselman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM