Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new to wcf rest services. I have created two method like Welcome and Hello. The two methods are working working in the browser like http://localhost:14502/MyRestService.svc/Hello/xxx"
and http://localhost:14502/MyRestService.svc/Welcome. But when i was consuming the service in the asp.net application by using jquery. It gives null result. Please give resolve that.

C#
[ServiceContract]
   public interface IMyRestService
   {
       [OperationContract]
       [WebGet( RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Welcome",BodyStyle=WebMessageBodyStyle.Wrapped)]
       string Welcome();
       [OperationContract]
       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Hello/{name}",BodyStyle=WebMessageBodyStyle.Wrapped)]
       string Hello(string name);

       [OperationContract]
       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Test?id={id}")]
       int Test(int id);

   }
 public class MyRestService : IMyRestService
   {

       public string Hello(string name)
       {
           return "Hello " + name;
       }

       public int Test(int id)
       {
           return id + 1;
       }

       public string Welcome()
       {
           return "Welcome to Rest Services";
       }
   }


The web.config file is


XML
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="RestService.MyRestService" behaviorConfiguration="ServiceBehaviour">
        <!-- end point-->
        <endpoint address="" binding="webHttpBinding" contract="RestService.IMyRestService" bindingConfiguration="webHttpBindingJsonP" behaviorConfiguration="web"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8001"/>
          </baseAddresses>
        </host>
      </service>
      <!---->      
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <dataContractSerializer  ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456"/>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingJsonP"  crossDomainScriptAccessEnabled="true"/>
        
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>


The Asp.net web page is
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConsumingWCFRestService.aspx.cs" Inherits="ConsumingWCFRestService" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $("#btnSubmit").click(function () {

                var url = "http://localhost:14502/MyRestService.svc/Welcome";
                $.getJSON(url, null, function (data) {
                    if (data != null) {
                        alert("success");
                    }
                    else {
                        alert(data);
                    }
                });
                
            });
           
        });
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="submit" id="btnSubmit" value="Call Rest" />
    </div>
    </form>
</body>
</html>



Please anybody resolve and reply to me...
Posted
Comments
Prasad Khandekar 1-Apr-13 11:14am    
Please try by changing the ajax call as $.getJSON(url, function (data) {

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