0

I'm writing my first web service class. I believe I've followed all of the directions from http://msdn.microsoft.com/en-us/library/bb398998.aspx, but the javascript fails because the namespace of the webservice is unknown.

My WebService:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BusinessLogicLayer;

namespace AVWebService
{    
    [WebService(Namespace = "http://MyTestSite.Com/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]

    public class FillDropLists : System.Web.Services.WebService
    {

        [WebMethod]
        public Lookup[] GetRooms(string DataCenterId)
        {
            return BLL.getRooms(DataCenterId).ToArray();
        }
    }
}

My Web Config

  <system.web>
     <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>      
        <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
     </httpHandlers>
  </system.web>

My ScriptManager is in my Master Page.

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="AVWebService.asmx" />
    </Services>
</asp:ScriptManager>

And finally, my javascript

function FillRoomsList(e) {
            var idx = e.selectedIndex;
            var dcId = e.options[idx].value;
            AVWebService.FillDropLists.GetRooms(dcId, OnRoomsReceived);
        }

The error I'm receiving, "AVWebService is undefined", would seem to indicate that the namespace isn't recognized at all.

Steve Wash
  • 986
  • 4
  • 23
  • 50

1 Answers1

0

You need to use ajax to integrate javascript with your web service. See the following link for an explanation on jQuery's implementation.

http://api.jquery.com/jQuery.ajax/

Example.

$.ajax({
     url : 'yourWebService.asmx',
     data: 'data you are sending to the web service'
     success: 'function to execute on success'
});
Brad M
  • 7,857
  • 1
  • 23
  • 40
  • I'll give this a try. I'm still reading and learning about AJAX/Web Services, and I understand the principals better then the details/protocols. Why does this web service require JQuery but none of the other examples I was reading use that? Is this just one way or is there something going on here I'm not aware of? – Steve Wash Apr 10 '13 at 15:44
  • You don't *have* to use jQuery, this was just an example of jQuery's implementation of ajax. If you look at the standard javascript API for xmlHttpRequests and the amount of code involved in making it cross-browser compatible, you'll see why people use jQuery. – Brad M Apr 10 '13 at 15:48
  • That's pretty much what I suspected. I was a bit surprised that I needed any additional framework, but of course the examples I was looking at were pretty much "Hello World" stuff. – Steve Wash Apr 10 '13 at 18:05
  • The `asp:scriptmanager` is supposed to auto-generate ajax/javascript code that enables you to use your webservice, but it's not working for some reason. Microsoft AJAX has it's pros and cons, but IMO you are better served by learning jQuery ajax. This way, you can apply that knowledge to *any* web service. – Brad M Apr 10 '13 at 18:12
  • So just to be clear, do I still need the entry in the ScriptManager? in the WebConfig? – Steve Wash Apr 10 '13 at 20:43
  • You technically don't need the scriptmanager or the changes to your web config. Just point the url of your ajax request to your .asmx file. .asmx is a bit old school though (although it still works fine). Look into WCF if you want the latest technology. – Brad M Apr 10 '13 at 20:45
  • I'm getting Error 500. There's something really funny going on. – Steve Wash Apr 10 '13 at 21:07
  • Create a new .asmx file, direct an ajax request to it, i.e. "/ajax/webService.asmx/myMethodName", and see what happens. – Brad M Apr 10 '13 at 21:26
  • I created a new webservice. I used the default as created by VS2010. The only line I changed was to uncomment out the [System.Web.Script.Services.ScriptService]. I hooked the Javascript call to the click even of a Panel on my Default page, just to be sure it wasn't something weird in that page. I still get the Error 500 from my default page. Maybe I should start thsi as a new question. It seems I've gone beyond what I originally asked. – Steve Wash Apr 10 '13 at 22:51
  • I shouldn't have marked this resolved (even though everyting you had told me was accurate and great info). I'm still having Error 500 issues. I can run the service in a browser itself and it works fine. If I remove the service I get Error 404. If the service is available, I can't call it without getting the 500. I did post this in a different question at http://stackoverflow.com/questions/15939524/calling-a-webservice-from-jquery-ajax-gets-error-500 – Steve Wash Apr 11 '13 at 13:23
  • Got it, finally. http://stackoverflow.com/questions/657313/request-format-is-unrecognized-for-url-unexpectedly-ending-in – Steve Wash Apr 11 '13 at 14:22