ASP.NET - Webservice not being called from javascript
- by Robert
Ok I'm stumped on this one.
I've got a ASMX web service up and running. I can browse to it (~/Webservice.asmx) and see the .NET generated page and test it out.. and it works fine.
I've got a page with a Script Manager with the webservice registered... and i can call it in javascript (Webservice.Method(...)) just fine.
However, I want to use this Webservice as part of a jQuery Autocomplete box. So I have use the url...? and the Webservice is never being called.
Here's the code.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class User : System.Web.Services.WebService {
public User () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Autocomplete(string q)
{
StringBuilder sb = new StringBuilder();
//doStuff
return sb.ToString();
}
web.config
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
And the HTML
$(document).ready(function() {
User.Autocomplete("rr", function(data) { alert(data); });//this works
("#<%=txtUserNbr.ClientID %>").autocomplete("/User.asmx/Autocomplete"); //doesn't work
$.ajax({
url: "/User.asmx/Autocomplete",
success: function() { alert(data); },
error: function(e) { alert(e); }
}); //just to test... calls "error" function
});