griegs
griegs

Reputation: 22760

Consume WCF Service with jQuery

I have a simple WCF service that looks like this;

[ServiceContract]
public interface IPostService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml)]
    string CheckAlive();

and...

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PostService : IPostService
{
    public string CheckAlive()
    {
        return "I'm here.";
    }

In my MVC Web Project I have a Service Reference to my webservice.

I then have jQuery code like this...

    function callWebMethod() {
        $.support.cors = true;
        $.ajax({
            type: "GET",
            url: "http://localhost:8732/Design_Time_Addresses/MyWebService/Service1/mex/CheckAlive",
            data: "{}",
            contentType: "application/soap+xml; charset=utf-8",

            success: function (msg) {
                alert("data:" + msg.d);
            }, error: function(a,b,c){ alert("error:" + b + ":" + c); }
        });
    }

However I always get a "Bad Request" error. I've never consumed a webservice with jQuery before and am floundering at this point.

Upvotes: 3

Views: 6848

Answers (2)

Rafay
Rafay

Reputation: 31033

your postservice class has to be decorated with the [ServiceContract]

namespace MyNamespace
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PostService : IPostService
    {
        public string CheckAlive()
        {
            return "I'm here.";
        }
    }
}

also have you defined the routes in the Global.ascx file

protected void Application_Start(object sender, EventArgs e)

    {

      RouteTable.Routes.MapServiceRoute<MyNamespace.PostService >("Service/SomeName");
    }

you can access the service like

      url:'http://loalhost:1234/Service/SomeName',

Upvotes: 0

Pritam Baldota
Pritam Baldota

Reputation: 41

Hey you have to check your endpoint configuration for WCF service. You can see this post for detailed information - http://www.codeproject.com/Articles/132809/Calling-WCF-Services-using-jQuery

Upvotes: 1

Related Questions