3

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.

5
  • possible duplicate of consume SOAP webservice using jquery Feb 29, 2012 at 4:46
  • possibly but i don't have an asmx file nor can i have one
    – griegs
    Feb 29, 2012 at 4:49
  • You put content type application/soap+xml, that's why I thought you had one - anyway, if you just want to read XML back from the endpoint with Ajax, see here: think2loud.com/224-reading-xml-with-jquery Feb 29, 2012 at 4:55
  • do you have a svc file? also the url seems incorrect, i think the parameter less method is called by default, how you have defined your routes?
    – Rafay
    Feb 29, 2012 at 4:55
  • i dont think you need the contentType in your ajax since your not doing a POST Mar 2, 2013 at 14:45

2 Answers 2

1

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

0
0

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',
4
  • 1
    My interface is decorated with [ServiceContract] and if i decorate the class with the same the service won't start.
    – griegs
    Feb 29, 2012 at 5:08
  • in that case you might have to decorate the interface members with the [OperationContract] also the implementation also have to have [OperationContract]
    – Rafay
    Feb 29, 2012 at 5:13
  • 1
    Each method is decorated with [OperationContract] in the interface. It's in the code in the question. Or do you mean to take the decorators out of the interface and into the class?
    – griegs
    Feb 29, 2012 at 5:15
  • can you try removing the [ServiceContract] from the interface and just place it on the class that is inheriting the interface also remove the [OperationContract] from the interface members
    – Rafay
    Feb 29, 2012 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.