How to call a service reference from jquery?

I have added a service reference to my asp.net web application (originally it was just a straight html site and we converted it to a web application)

We need to access a WCF service via jquery/ajax and not from within any .cs files. So far we haven't been able to get jquery to hit the service by any means, even before it was converted to a web app (converted in hopes that it would be easier to add service reference and call it)

I have the WCF service running in a separate solution running on my desktop and the web app open separately. My service is called SchoolService and is located in the Service References folder.

How in jquery do I call that service reference?

This is what we used from a demo that also did not work:

<script type="text/javascript">
    $(document).ready(function () {
        var schoolsCache = {}, lendersCache = {}, schoolsXhr, lendersXhr;

        $('#Schools').autocomplete({
            source: function (request, response) {
                var term = request.term;
                if (term in schoolsCache) {
                    response(schoolsCache[term]);
                    return;
                }
                if (schoolsXhr != null) {
                    schoolsXhr.abort();
                }
                schoolsXhr = $.getJSON('SchoolService.svc/GetSchools', request, function (data, status, xhr) {
                    schoolsCache[term] = data.d;
                    if (xhr == schoolsXhr) {
                        response(data.d);
                        schoolsXhr = null;
                    }
                });
            }
        });
     });
</script>

I have also tried this line which did not work:

schoolsXhr = $.getJSON('http://localhost:8000/SchoolService/GetSchools', request, function (data, status, xhr) {

Here is the interface in my WCF sdervice:

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using MyApp.DomainModel;

namespace MyAppService
{
        [ServiceContract]
        public interface ISchoolService
        {
                [OperationContract]
                [WebGet(ResponseFormat = WebMessageFormat.Json)]
                IList<School> GetSchools(string term);

                [OperationContract]
                [WebGet(ResponseFormat = WebMessageFormat.Json)]
                School GetSchool(string schoolName);
        }
}

What are the necessary specific steps needed to make this work? The goal is to let user type into textbox, use jquery autocomplete, which uses the ajax call to service to pull back data that contains the typed text... surely this has been done before?

CD

Answers


You can take a look at this CodeProject article. The author is discussing exactly this profile a WCF service and JQuery client.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.