Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a WCF service and hosted in IIS.

WCF service--


C#
 [ServiceContract]
 public interface IService1
 {
     [OperationContract]
     [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
     string GetData(int value);

     [OperationContract]
     [WebInvoke(Method ="POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
     string[] GetUser(string Id);

 }


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 public class Service1 : IService1
 {
     public string GetData(int value)
     {
         return string.Format("You entered: {0}", value);
     }


     public string[] GetUser(string Id)
     { return new User().GetUser(Convert.ToInt32(Id)); }
 }


Web Config of WCF--

XML
<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="DemoService.Service1">
        <endpoint address="DemoSevice" binding="webHttpBinding" bindingConfiguration="" contract="DemoService.IService1"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8090"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>


after that I hosted this service in IIS and now I want to use this service using J Query.
J query code--
JavaScript
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>

     <script type="text/javascript">

         var Type;
         var Url;
         var Data;
         var ContentType;
         var DataType;
         var ProcessData;
         //Generic function to call AXMX/WCF  Service        
         function CallService() {
             debugger;
             $.ajax({
                 type: Type, //GET or POST or PUT or DELETE verb
                 url: Url, // Location of the service
                 data: Data, //Data sent to server
                 contentType: ContentType, // content type sent to server
                 dataType: DataType, //Expected data format from server
                 processdata: ProcessData, //True or False
                 dataFilter: function (data) { return data; },
                 success: function (msg) {
                     debugger;
                     var jsonData = jQuery.parseJSON(msg);
                     ServiceSucceeded(jsonData);
                 },

                 error: ServiceFailed// When Service call fails
             });
         }

         function ServiceFailed(result) {
             alert('Service call failed: ' + result.status + '' + result.statusText);
             Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
         }

         function WCFJSON() {
             var uesrid = "1";
             Type = "POST";
             Url = "http://localhost:8090/DemoService/Service1.svc/GetUser";
             Data = '{"Id": "' + uesrid + '"}';
             ContentType = "application/json; charset=utf-8";
             DataType = "json";
             ProcessData = true;
             CallService();
         }

         function ServiceSucceeded(result) {

             if (DataType == "json") {

                 resultObject = result.GetUserResult;

                 for (i = 0; i < resultObject.length; i++) {
                     alert(resultObject[i]);
                 }

             }

         }

         function ServiceFailed(xhr) {
             alert(xhr.responseText);
             if (xhr.responseText) {
                 var err = xhr.responseText;
                 if (err)
                     error(err);
                 else
                     error({ Message: "Unknown server error." })
             }
             return;
         }

         $(document).ready(
         function () {
             WCFJSON();
         }
         );

    </script>


But this block of j query is not working in my end. Where I am doing mistake?. Any idea will be appreciated.
Posted
Comments
Rajat_RJT 6-Apr-15 1:40am    
what error you are getting?

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