Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello to all..

i wana consume my wcf service in HTMl pages through jquery... for this..i seen some code's but its not working .. i dint getting where am doing mistake..

here my code

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

namespace WcfService1
{
 

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        List<RunDto> GetTeamData(int teamId, int weekNumber);
    }

    [DataContract]
    public class RunDto
    {
        public string name1 = "SampleName";
        [DataMember]
        public string name
        {
            get { return name1; }
            set { name1 = value; }
        }
      
    }
   
}


Service1.svc.cs

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

namespace WcfService1
{
   
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        List<RunDto> ls = new List<RunDto>();

        public List<RunDto> GetTeamData(int teamId, int weekNumber)
        {
            RunDto rn = new RunDto();
            rn.name = teamId + " s " + weekNumber;
            ls.Add(rn);
            return ls;
           
        }
    }
}


web.config

C#
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SampleService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="IService1" name="samendpoint" behaviorConfiguration="AjaxBehaviour">
       
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"   />
      </service>
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
         <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehaviour">
          <webHttp/>
         
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <!--<modules runAllManagedModulesForAllRequests="true" />-->
    <directoryBrowse enabled="true" />
  </system.webServer>
  
</configuration>



jquery call in Html page

C#
function GetTeamData(teamId, weekNumber) {
    $.ajax(
    {
        async: true,
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "../../Services/RunningBarbusService.svc/GetTeamData",
        dataType: "json",
        data: '{"teamId":"' + teamId + '", "weekNumber":"' + weekNumber + '"}',
        success: function (content) {
            DisplayRun(map, content);
        }
    });
}


1st while excuting service only its getting error lik this

C#
Error: Cannot obtain Metadata from http://localhost:50807/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:50807/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:50807/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:50807/Service1.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:50807/Service1.svc    The HTML document does not contain Web service discovery information.


i need to consume my service from html page../. any possible ways... or any modifications to my code..

thanks.. in advance
Posted
Comments
Prasad Khandekar 18-Jun-13 6:02am    
Hello Prasad,

Have you tried going through the documentation links provided in error. Please also have a look at this thread (http://stackoverflow.com/questions/7192877/selfhosted-wcf-service-cant-be-tested-via-wcftestclient).

Regards,
Lokesh Kondapalli 19-Jun-13 0:08am    
thq u for this infor.... but so many times i tested in wcftestclient... but with webHttp binding its getting error...

basicaly i need to consume wcf service from my html pages... soo any possible ways ?

1 solution

As you are running the Service directly, it is running the WCFTestClient by default.

WCFTestClient does not support webHttpBinding.
That's why it is throwing the exception. If you will host the Service in IIS, you can browse the Service file directly without any errors (provided there are no other issues).

So, to test your html page, just run that html page directly.
And put some debuggers inside the jQuery functions and check in browser (FireBug in FireFox), if it calling the Service and returning values as expected or not.

Let me know, if you face any issues further.
 
Share this answer
 
Comments
Lokesh Kondapalli 18-Jun-13 6:29am    
thq u for giving this information... and hav u seen my web.config file... is there any error in that.... or in my jquery call..

any best link for this
I don't think there are any errors here. Are you getting any error while running the html?
Have you debugged?
Lokesh Kondapalli 18-Jun-13 6:56am    
yes but am not getting any result..

i hosted in my local iis n just browsed it.. in that its showing ur service not exposing meta data... n while consuming in my html page.. no result coming from that.... hav u tested my sample code ?
Lokesh Kondapalli 18-Jun-13 6:58am    
my simple requirement is to transfer data from html to service.. and save to db from service.. but its getting errors... do u hav any sample code to post our data to service from html page..
As you are facing exception while browsing, then there are some other issues.
I have not tried your code. I am busy now, I can check tomorrow.

So, I would suggest you to search for the exception in Google and try the solutions.

As I suggested before, try to put debugger in jQuery code and see where you are facing the issue. Otherwise I will try tomorrow.

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