0

I have this code:

$('#' + textboxID).autocomplete({ delay: delay, source: ["cats", "dogs"] });

which works fine.

I want to use a webservice:

$('#' + textboxID).autocomplete({ delay: delay, source: webserviceURL});

This doesn't work. My webservice URL is /blah/blah.asmx/myMethod

The webservice definition is:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<string> myMethod(string term)
{
       // Logic here, return list of strings
}

The method never gets called. Can anyone point me in the right direction? I have other webservices in the same .asmx which work fine with other controls (other controls are using $.ajax to call them).

Edit: Getting closer, I now get a 500 error saying Request format is unrecognized for URL unexpectedly ending in '/myMethod'.

I think it's not treating my url quite right...

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • 1
    Have you used a tool like fiddler to see whats been sent/received by the browser? Can you hit the webservice address manually in the browser address bar? – Geoff Appleford Mar 24 '11 at 16:45
  • I have no clue why I didn't use fiddler to start with, the problem is it is calling my webservice like a http request, with `?term=blah` it would seem, will do more investigation! – NibblyPig Mar 24 '11 at 16:52
  • sounds like you need to setup the ajax options. I imagine you need to set content type to JSON and make sure it is Post not Get. Additionally make sure you uncommented the line of code near the beginning of the web service class that allows the class to be exposed to client side script. – The Muffin Man Mar 24 '11 at 17:04

2 Answers2

1

Here's a SO thread with a solution to using it with an Asp.net webservice:

jQuery AutoComplete (jQuery UI 1.8rc3) with ASP.NET web service

Community
  • 1
  • 1
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
  • Looks good but there is some kind of discrepancy between the webservice url parameter of $.ajax and the one you supply with autocomplete, I am not sure why it's treating them differently, but all my $.ajax calls work fine just as the example above – NibblyPig Mar 24 '11 at 17:04
  • @SLC - the urls look the same as you are using. I think you need to configure the $.ajax options correctly and possibly add ` ` to your web.config. See http://forums.asp.net/t/988377.aspx/2/10?Request+format+is+unrecognized+for+URL+unexpectedly+ending+in+GetEmployees+ – Geoff Appleford Mar 24 '11 at 17:15
0

Solved it, the key was in this question:

How do I set JQuery Autocomplete to POST instead of GET?

I put $.ajaxSetup( { type: "post" } ); before my .autocomplete line and it solved it. Most annoying they didn't put it as an option!

Community
  • 1
  • 1
NibblyPig
  • 51,118
  • 72
  • 200
  • 356