0

I just tried to Make a solution on VS 2010 to practice consuming REST services with JQuery. I used the template WCF REST Service Application. It created a class that I named Services.cs with a method that I would like to call:

  [WebGet(UriTemplate = "")]
    public List<SampleItem> GetCollection()
    {
        SampleItem item;
        List<SampleItem> lst = new List<SampleItem>();
        for (int i = 0; i < 12; i++)
        {
            item = new SampleItem { Prom = 1960 + i, Name = string.Format("Name {0}", i.ToString()) };
            lst.Add(item);
        }
        return lst;
    }

the Global.asax.cs looks like: void Application_Start(object sender, EventArgs e) { RegisterRoutes(); }

    private void RegisterRoutes()
    {
        // Edit the base address of Service1 by replacing the "Service1" string below
        RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service)));
    }

Then I made a new asp.net Empty Web Site, added the jquery files etc. In the default.htm page I like to call GetCollection, so I added the following jquery code.

    $(function () {
        var serviceUrl = ??????
        $.ajax({
            url: serviceUrl,
            type: 'POST',
            data: '{}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (colD) {
                var col = colD.d;
            }

        });
    });

what URl shoult I put? especially I have hard time to figure out the port on my hostlocal. I tried http://localhost:5187/KendoRestService/GetCollection, Where KendoRestService is the name of the REST project, and the namespace on service.cs. Any help would be greatly appreciated.

2 Answers 2

0

I believe you'd want to give [WebGet(UriTemplate = "")] a value i.e [WebGet(UriTemplate = "getCollection")]

Then the javascript url would be:

var serviceUrl = ServiceName/UriTemplate

so in your case:

var serviceUrl = '/getCollection';
5
  • No it didn't work. I put a beakpoint to getCollection but it never goes there, nor on success part of ajax call. There are 2 projects in my solution. Mar 13, 2012 at 20:44
  • Have you tried changing the parameter typeof(Service) to point to the dll GetCollection() is in? Mar 13, 2012 at 21:08
  • Not in the js. Your problem sounds to be related to routing. What kind of error are you getting from the ajax request? The change is in the global.asax file: RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service))); Mar 14, 2012 at 12:38
  • Justin, I added error handling to my ajax call: error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); xhr.status = 0, and thrownError=''; my problem is port number that I don't know where to init. Your solution can't work since my service is located in another project and /getCollection cannot be found. Thanks anyway. Anyone else who has already done jquery to REST WCF? Mar 14, 2012 at 17:18
  • Unless your projects are on separate ports it will work. Is your other project building as a separate dll? Mar 14, 2012 at 17:40
0

Ok I found the port number. Check the properties of the Service Project Alt+Enter, Web Tab, Under Servers check Apply server settings to all users , and Use Visual Studio Development Server, Auto-assign Port. There is a port number in front of Specific port option. This port number will be used by Visual Studio.

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.