Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone,

I need some help in how to call hosted WCF service from JQuery

I need to host this service and call the hosted WCF service from Jquery from another application...

My code as below..

IService1.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace WCF_REST_SOAP_JQuery
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]    
    public interface IService1
    {
        [OperationContract]
        string SayHello(string name);
    }
}


Service1.svc

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;


namespace WCF_REST_SOAP_JQuery
{
    
    [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]   
    public class Service1 : IService1
    {
        [WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
        public string SayHello(string name)
        {
            return string.Format("Hello {0}", name);
        }
    }
}


Web.config
XML
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
      <services>
        <service name="WCF_REST_SOAP_JQuery.Service1"
                 behaviorConfiguration="ServiceBehaviour">
          <endpoint address="JSON" binding="webHttpBinding" contract="WCF_REST_SOAP_JQuery.IService1"
                    behaviorConfiguration="JSONEndpointBehaviour"
                    bindingConfiguration="" name="RESTEP"></endpoint>

          <endpoint address="SOAP" binding="basicHttpBinding" contract="WCF_REST_SOAP_JQuery.IService1"
                     name="basic"></endpoint>

          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
          
        </service>
      </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="JSONEndpointBehaviour">              
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          
            <serviceBehaviors>
                <behavior name="ServiceBehaviour">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
    </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value= "GET, POST"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>



please some one help in this.
Posted
Updated 3-Jul-14 23:41pm
v2

JavaScript
$.ajax({
  url: "https://192.168.1.11/UserService-20120830/GetUserService.svc/REST/",
  type: "post",
  dataType: "json",
  data: {},


It might help you
Call a wcf service from Jquery[^]
 
Share this answer
 
v2

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