Consume Web Service from HTML (without using the .htc component)

Last post 06-04-2008 11:16 AM by Pyrrhonist. 10 replies.

Sort Posts:

  • Consume Web Service from HTML (without using the .htc component)

    05-13-2008, 4:24 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    We have an intranet site (several hundred pages) that we're converting to a standard DWT (Dynamic Web Template).  To help facilitate this, we're looking for a way to dynamically populate some of the page content from various databases.  I've been able to accomplish this within an ASP.Net application, but that isn't an option for use with the several hundred static .htm files we also need to convert.

     So here's what I'm looking at:

    anypage.htm

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Testing HTML Access to Web Service</title>

        <script src="http://someserver/scripts/MicrosoftAjax.js" type="text/javascript"></script>
        <script src="http://someserver/webservice.asmx/js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
          function GetTime() {
            WebService.GetTime(onSuccess, onFailure);
          }

          function onSuccess(result) {
              $('Time').innerHTML = result;
          }

          function onFailure(result) {
            alert(result.get_message());
          }
        </script>
    </head>
    <body>
    <h1>Testing HTML consumption of web services</h1>
    <form id="frm">
    <input id="Button1" type="button" value="Get Time" onclick="GetTime()" /><br />
    <div id="Time"></div><br />
    </form>
    </body>
    </html>

    WebService.asmx

    using System;
    using System.Collections;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;

    [WebService(Namespace = "WebService")]
    [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 WebService : System.Web.Services.WebService {

        public WebService () {

            //Uncomment the following line if using designed components
            //InitializeComponent();
        }

        [WebMethod]
        public string GetTime() {
          return DateTime.Now.ToString();
        }
    }

    Doing some debugging and walking through the various steps, everything SEEMS to be working as it should...however, result keeps returning a 404 error (if you look at the get_status() portion of the result object) with the message that "The server method 'GetTime' failed".

     I'm presently running the .htm file from my development box (localhost) and pointing to the development server (someserver).  I was lead to believe that using the MicrosoftAjax.js approach (above) allowed for the cross domain scripting issues?  Is this even that?  Found oodles of comments / how-to regarding the .htc component method to do the same thing, but that's really not an option we can use in this environment.  Thoughts?  Suggestions?  Sanity??  **grin**

  • Re: Consume Web Service from HTML (without using the .htc component)

    05-15-2008, 1:28 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    So...after stepping further through the MicrosoftAjax.js function to determine where my error was coming from, I discovered why I was getting my 404 error.

    What is happening, is that the asmx/js file that is returned (line 27 of the code snippet above) does NOT in fact return a fully realized URL&I actually fudged the one thats pasted in there from memory.  What you end up with is just the /webservice.asmx part of the URL.  What the MicrosoftAjax.js does, since the full URL is not provided, is gets the URL from the base page (in my case localhost on my dev box) and appends that to the part listed in the proxy class.  This results in http://localhost/webservice.asmx , which does not exist&which results in my 404 error!

    That being solved gets me further, however when I place the proper URL into the proxy.js file I run into the cross domain script error&which is what I was trying to avoid from step one!  **laugh**

    So&now it sounds like a totally separate issue!  

    I'm still trying to access a web service hosted on one machine (server 1) from a static html page on a different machine (server 2) without using the .htc component OR causing the cross domain script error.  Is this even possible???  

  • Re: Consume Web Service from HTML (without using the .htc component)

    05-27-2008, 12:04 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    Still no luck?  Any further suggestions from anyone?

     As an aside, I started out by following this post

    http://forums.asp.net/t/1169271.aspx

    Which people claim does in fact work...anyone else have other options / suggestions for me?  Bueller?  Bueller? ;)

  • Re: Consume Web Service from HTML (without using the .htc component)

    05-27-2008, 1:36 PM
    • Loading...
    • gibble
    • Joined on 05-15-2007, 4:40 PM
    • Posts 36

    I believe you are missing the [ScriptMethod] attribute on your GetTime function.

    Also, you can't reference cross domain web services.  You have to create a local web service that calls the remote web services. 

     

    Basically just a proxy/wrapper.

  • Re: Consume Web Service from HTML (without using the .htc component)

    05-27-2008, 2:54 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    Thanks for the reply...placing the [ScritpMethod (ResponseFormat=ResponseFormat.JSON)] in my web service didn't seem to work...or at least nothing further was advanced.


    Isn't the call to the web service with the /js appended to the end supposed to instantiate a local instance of a javascript proxy class that you can then use to bypass the cross-domain scripting policy? 

  • Re: Consume Web Service from HTML (without using the .htc component)

    05-27-2008, 6:01 PM
    • Loading...
    • gibble
    • Joined on 05-15-2007, 4:40 PM
    • Posts 36

    I tried for quite some time, to get cross domain web service calls to work from js, and could not, as soon as the web service was moved to the local domain things worked.

     Try creating one locally, and if it works, at least you can narrow down where the problem lies.     
     

  • Re: Consume Web Service from HTML (without using the .htc component)

    06-03-2008, 5:24 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    Sorry for the long time between replies / follow ups, but I was out of the country on vacation until recently...

     Turns out the server that the .htm files are hosted on does not allow anything else to work (no .Net enabled in IIS at all)....HOWEVER, someone, in all their brilliance, has enabled .asp pages!  **laugh**

    Anyways, security folks broke it down like this for me... aspx BAD...asp GOOD...not sure what they were thinking, but I'm certain the inventive coder / hacker could accomplish just as much in classic ASP as in a .net app...either way, I'm now looking at something somewhat different...

     Any links / suggestions on how to consume a web service in classic ASP???

  • Re: Consume Web Service from HTML (without using the .htc component)

    06-03-2008, 5:56 PM
    • Loading...
    • gibble
    • Joined on 05-15-2007, 4:40 PM
    • Posts 36

    Time to put it on a server that supports aspx.  New development in asp is like buying a '86 Chevette as a "new" car.

  • Re: Consume Web Service from HTML (without using the .htc component)

    06-03-2008, 6:05 PM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    **laugh**  Love the imagery of that analogy **grin**

    Problem is this though...the security folks have a server (IISv6.0) dedicated to serving up ONLY static HTML (Web service extensions---> .Net 1.x and 2.0--->Prohibited).  They've locked the server settings up tight (as there is 'public' access to this server) so that .Net applications cannot be run from it...as mentioned though, they DO allow classic ASP (Web service extensions--->Active Server pages-->enabled) pages to be served up...

     Us developers have our own server on our own domain which we look after.  Once again, I'm trying to access a .Net web service (or WCF Service) on our development domain from the static HTML host domain to attempt to push dynamic content into the static content.

     I'm beginning to believe this really isn't a simple solution situation though :(  (ie. short of just dropping all .html pages as 'simple' **grin**)

  • Re: Consume Web Service from HTML (without using the .htc component)

    06-04-2008, 11:02 AM
    • Loading...
    • gibble
    • Joined on 05-15-2007, 4:40 PM
    • Posts 36

    I don't understand how .asp files are considered safer for external access than .NET .aspx files?

     .NET is much more secure than classic .asp could ever hope to be.

    When you say public access do you mean writing to the file system?  Or that it's pages are accessible to the public?  If it's the former, then I can sort of see the reasoning, but should be able to only allow .NET for your site, and still prohibit it from others.
     

    As far as consuming web services in .asp files, I haven't a clue.  I've been using .NET nearly exclusively since it came out, and never messed with web services in .asp prior to that.         

  • Re: Consume Web Service from HTML (without using the .htc component)

    06-04-2008, 11:16 AM
    • Loading...
    • Pyrrhonist
    • Joined on 05-13-2008, 2:56 PM
    • Victoria, British Columbia
    • Posts 7

    I totally agree with your comment about .Net being more secure than classic ASP...but security people are security people...and you just do what they tell you **grin**

    You're correct with your assumption that our 'public access' does allow writing to the file system.  The public HTML server has front page extensions enabled and a rather large user group have been publish rights to keep their own intranet sites up to date...thus the hundreds (thousands) of static HTML pages we need to try and 'update' with some dynamic content.

     Since they've locked down the IIS server to prohibit .Net entirely, I'm not sure if there's a workaround to allow it on a single site only...I've tried a few different 'hacks' on the IIS server but nothing seemed to pan out.  I was told that IIS over rules any of the permissions I set within the web site, so no matter what I do to the file system behind, it will not be served up by IIS at all.

    I'm in the same boat as yourself regarding classic ASP...it's been two+ years since I've even thought about looking at the stuff...ah well...guess that's where the interenet and <insert search engine of choice here> come in handy ;)

Page 1 of 1 (11 items)