Tuesday, 13 March 2007

Issues calling web services with Ajax

I have created a video demoing calling web services from Ajax and asked a work colleague to try it out. He was getting a strange JavaScript error "Microsoft JScript runtime error: '_staticInstance' is null or not an object":-
image1
When I intellisensed the search variable it is not an object like I would have expected, but instead its the paramater I was passing into the web service
image2
This was the code of the web service:-

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for Search
/// </summary>
[WebService(Namespace = "http://localhost/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Search : System.Web.Services.WebService
{
public Search()
{
}
[WebMethod(Description = "Retrieves list of contacts and organisations.")]
public string GetAllSearchResults(string ADPersonName, string Search)
{
...
}
[WebMethod(Description = "Retrieves list of contacts.")]
public string GetContactSearchResults(string ADPersonName, string Search)
{
...
}
[WebMethod(Description = "Retrieves list of organisations.")]
public string GetOrganisationSearchResults(string ADPersonName, string Search)
{
...
}
}

As you can see the web service class is called "Search" but so is the paramater for the web service - the javascript generated for the webservice assumes they are the same object, which causes the above error!

Simply changing the paramater name fixed the problem - but one to beware of!

Enjoy

Ross:)

No comments: