Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
how to resolve cross-domain issues using wcf services


below is my code :
JavaScript
$.ajax({
               type: "POST",
               contentType: "application/json; charset=utf-8",
               url: 'http://localhost:49878/Employee.svc/GetEmployeeList',

               dataType: "json",
               success: function (data) {
                   //alert(data.d);
                   $($.parseJSON(data.d)).each(function (index, value) {
                       $("#TableID").append("<tr><td>" + value.EmpId + "</td><td>" + value.EmpName + "</td><td>" + value.Desgination + "</td></tr>");
                   });
               },
               error: function (result) {
                  // alert(result);
               }
           });



Services Code:

C#
private static void ResponseHeaders()
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add(
                          "Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
                          "Access-Control-Allow-Methods", "POST"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
                          "Access-Control-Allow-Headers", "Content-Type, Accept");
        }
        public List<Employe> GetEmployeeList()
        {
            var listEmployee = new TestEntities().Employes.ToList();
              ResponseHeaders();
            return listEmployee;
        }
Posted
Updated 28-Dec-15 0:55am
v2
Comments
Ashutosh shukla207 28-Dec-15 6:11am    
tell one thing ... is there a problem in writing wcf ? or in calling the service from another domain? . and u are calling throw c# code ? or from jquery ajax() function? if u are acceding the wcf service in c# than nothing more to do . u can call also asmx service as well as wcf. by configuring the iis server from web.config. just google . best of luck .



but in case of jquery ajax() , u have to configure iis setting to allow calling for service methods from another domain. go throw web.config file
awaisshabir 28-Dec-15 6:17am    
i am calling wcf services from another domain using jquery ajax

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900