Click here to Skip to main content
15,442,578 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 application by using jquery. It gives null result. Please give resolve that.

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

       [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 version="1.0"?>

    <compilation debug="true" targetFramework="4.0" />
      <service name="RestService.MyRestService" behaviorConfiguration="ServiceBehaviour">
        <!-- end point-->
        <endpoint address="" binding="webHttpBinding" contract="RestService.IMyRestService" bindingConfiguration="webHttpBindingJsonP" behaviorConfiguration="web"></endpoint>
            <add baseAddress="http://localhost:8001"/>
        <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 name="web">
          <dataContractSerializer  ignoreExtensionDataObject="true" maxItemsInObjectGraph="123456"/>
        <binding name="webHttpBindingJsonP"  crossDomainScriptAccessEnabled="true"/>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
    <modules runAllManagedModulesForAllRequests="true"/>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <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) {
                    else {
    <form id="form1" runat="server">
    <input type="submit" id="btnSubmit" value="Call Rest" />

Please anybody resolve and reply to me...
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