ASP Page as Remote Function call from Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Waller

    ASP Page as Remote Function call from Javascript

    Hello,

    I've been playing with the idea of just how to use an ASP page to provide a
    remote function call. In an ideal world this would be a web service but how
    can you do it if restricted to ASP 3.0 ?

    Idea 1 was to write an ASP page that accepted arguments using the classic
    ?X=1&Y=2 type of strings and have the page return an ADO disconnected record
    set containing the results of the action. This is call very well, and works,
    but you do have to allow the browser to create an ADODB.RecordSet in which
    to place the results which goes against some security principles.

    Idea 2 was to use an <IFRAME> and replace the src for this frame at run time
    with the result from an ASP page, again using the ?X=1&Y=2 type of string to
    pass in arguments. The results could then come back as a table and be
    accessed via the DOM. The problem with this is that the client needs to wait
    for the page to be loaded before it can be sure that the contents of the
    table is valid and the browser does not seem to allow waits and does not
    like busy waits.

    Idea 3 - I'm still looking for this ! Does anyone have any ?

    Many thanks

    Martin


  • Bob Barrows [MVP]

    #2
    Re: ASP Page as Remote Function call from Javascript

    Martin Waller wrote:[color=blue]
    > Hello,
    >
    > I've been playing with the idea of just how to use an ASP page to
    > provide a remote function call. In an ideal world this would be a web
    > service but how can you do it if restricted to ASP 3.0 ?
    >
    > Idea 1 was to write an ASP page that accepted arguments using the
    > classic ?X=1&Y=2 type of strings and have the page return an ADO
    > disconnected record set containing the results of the action. This is
    > call very well, and works, but you do have to allow the browser to
    > create an ADODB.RecordSet in which to place the results which goes
    > against some security principles.
    >
    > Idea 2 was to use an <IFRAME> and replace the src for this frame at
    > run time with the result from an ASP page, again using the ?X=1&Y=2
    > type of string to pass in arguments. The results could then come back
    > as a table and be accessed via the DOM. The problem with this is that
    > the client needs to wait for the page to be loaded before it can be
    > sure that the contents of the table is valid and the browser does not
    > seem to allow waits and does not like busy waits.
    >
    > Idea 3 - I'm still looking for this ! Does anyone have any ?
    >
    > Many thanks
    >
    > Martin[/color]

    If I understand correctly what you're after, then the answer is XMLHTTP.
    Google should supply the rest ...

    Bob Barrows
    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Comment

    • Martin Waller

      #3
      Re: ASP Page as Remote Function call from Javascript

      Hello,

      Many thanks for the reply but I'm not sure how that gets around having to do
      something like:

      x = new ActiveXObject(" Microsoft.XMLHT TP");

      in the JavaScript which requires the setting / unsetting of a security
      option in the browser ?

      Martin

      "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
      news:u$a2hHWpEH A.3196@tk2msftn gp13.phx.gbl...[color=blue]
      > Martin Waller wrote:[color=green]
      > > Hello,
      > >
      > > I've been playing with the idea of just how to use an ASP page to
      > > provide a remote function call. In an ideal world this would be a web
      > > service but how can you do it if restricted to ASP 3.0 ?
      > >
      > > Idea 1 was to write an ASP page that accepted arguments using the
      > > classic ?X=1&Y=2 type of strings and have the page return an ADO
      > > disconnected record set containing the results of the action. This is
      > > call very well, and works, but you do have to allow the browser to
      > > create an ADODB.RecordSet in which to place the results which goes
      > > against some security principles.
      > >
      > > Idea 2 was to use an <IFRAME> and replace the src for this frame at
      > > run time with the result from an ASP page, again using the ?X=1&Y=2
      > > type of string to pass in arguments. The results could then come back
      > > as a table and be accessed via the DOM. The problem with this is that
      > > the client needs to wait for the page to be loaded before it can be
      > > sure that the contents of the table is valid and the browser does not
      > > seem to allow waits and does not like busy waits.
      > >
      > > Idea 3 - I'm still looking for this ! Does anyone have any ?
      > >
      > > Many thanks
      > >
      > > Martin[/color]
      >
      > If I understand correctly what you're after, then the answer is XMLHTTP.
      > Google should supply the rest ...
      >
      > Bob Barrows
      > --
      > Microsoft MVP -- ASP/ASP.NET
      > Please reply to the newsgroup. The email account listed in my From
      > header is my spam trap, so I don't check it very often. You will get a
      > quicker response by posting to the newsgroup.
      >
      >[/color]


      Comment

      • Mark Schupp

        #4
        Re: ASP Page as Remote Function call from Javascript

        How about this (uses framesets):

        1. Main frame directs "communicat ion" frame to target asp page and then
        directs itself to a "waiting" page.
        2. Page generated by target asp page includes JavaScript code to redirect
        main frame back to original state.

        --
        Mark Schupp
        Head of Development
        Integrity eLearning
        Advancing limitless knowledge and continual growth to create confident problem solvers, one course at a time.



        "Martin Waller" <Martin.Waller@ NOSPAN.com> wrote in message
        news:eNpja6WpEH A.1588@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hello,
        >
        > Many thanks for the reply but I'm not sure how that gets around having to[/color]
        do[color=blue]
        > something like:
        >
        > x = new ActiveXObject(" Microsoft.XMLHT TP");
        >
        > in the JavaScript which requires the setting / unsetting of a security
        > option in the browser ?
        >
        > Martin
        >
        > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
        > news:u$a2hHWpEH A.3196@tk2msftn gp13.phx.gbl...[color=green]
        > > Martin Waller wrote:[color=darkred]
        > > > Hello,
        > > >
        > > > I've been playing with the idea of just how to use an ASP page to
        > > > provide a remote function call. In an ideal world this would be a web
        > > > service but how can you do it if restricted to ASP 3.0 ?
        > > >
        > > > Idea 1 was to write an ASP page that accepted arguments using the
        > > > classic ?X=1&Y=2 type of strings and have the page return an ADO
        > > > disconnected record set containing the results of the action. This is
        > > > call very well, and works, but you do have to allow the browser to
        > > > create an ADODB.RecordSet in which to place the results which goes
        > > > against some security principles.
        > > >
        > > > Idea 2 was to use an <IFRAME> and replace the src for this frame at
        > > > run time with the result from an ASP page, again using the ?X=1&Y=2
        > > > type of string to pass in arguments. The results could then come back
        > > > as a table and be accessed via the DOM. The problem with this is that
        > > > the client needs to wait for the page to be loaded before it can be
        > > > sure that the contents of the table is valid and the browser does not
        > > > seem to allow waits and does not like busy waits.
        > > >
        > > > Idea 3 - I'm still looking for this ! Does anyone have any ?
        > > >
        > > > Many thanks
        > > >
        > > > Martin[/color]
        > >
        > > If I understand correctly what you're after, then the answer is XMLHTTP.
        > > Google should supply the rest ...
        > >
        > > Bob Barrows
        > > --
        > > Microsoft MVP -- ASP/ASP.NET
        > > Please reply to the newsgroup. The email account listed in my From
        > > header is my spam trap, so I don't check it very often. You will get a
        > > quicker response by posting to the newsgroup.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Martin Waller

          #5
          Re: ASP Page as Remote Function call from Javascript

          Hello,

          One way I've found to get away from the requirement in JavaScript to perform
          the new ActiveXObject call is to put the object in the HTML page as it gets
          sent to the client and then have the JavaScript call that. See below for an
          example. To run this the URL will need to be changed to reflect where you
          put it ! In my case it was called junk.asp...

          <html>
          <head>
          <script language="JavaS cript">

          function Do()
          {
          alert("Do()");
          penguin.open("g et","http://localhost/smartanalysis/junk.asp",false );
          alert(1);
          penguin.send();
          alert(2);
          alert(penguin.r esponseText);
          }

          </script>
          <object
          classid="clsid: F6D90F16-9C73-11D3-B32E-00C04F990BB4"
          id="penguin">
          </object>
          </head>
          <body onLoad = "Do()">
          </body>
          </html>

          Martin
          "Mark Schupp" <mschupp@ielear ning.com> wrote in message
          news:u$ymN#WpEH A.348@TK2MSFTNG P15.phx.gbl...[color=blue]
          > How about this (uses framesets):
          >
          > 1. Main frame directs "communicat ion" frame to target asp page and then
          > directs itself to a "waiting" page.
          > 2. Page generated by target asp page includes JavaScript code to redirect
          > main frame back to original state.
          >
          > --
          > Mark Schupp
          > Head of Development
          > Integrity eLearning
          > www.ielearning.com
          >
          >
          > "Martin Waller" <Martin.Waller@ NOSPAN.com> wrote in message
          > news:eNpja6WpEH A.1588@TK2MSFTN GP09.phx.gbl...[color=green]
          > > Hello,
          > >
          > > Many thanks for the reply but I'm not sure how that gets around having[/color][/color]
          to[color=blue]
          > do[color=green]
          > > something like:
          > >
          > > x = new ActiveXObject(" Microsoft.XMLHT TP");
          > >
          > > in the JavaScript which requires the setting / unsetting of a security
          > > option in the browser ?
          > >
          > > Martin
          > >
          > > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
          > > news:u$a2hHWpEH A.3196@tk2msftn gp13.phx.gbl...[color=darkred]
          > > > Martin Waller wrote:
          > > > > Hello,
          > > > >
          > > > > I've been playing with the idea of just how to use an ASP page to
          > > > > provide a remote function call. In an ideal world this would be a[/color][/color][/color]
          web[color=blue][color=green][color=darkred]
          > > > > service but how can you do it if restricted to ASP 3.0 ?
          > > > >
          > > > > Idea 1 was to write an ASP page that accepted arguments using the
          > > > > classic ?X=1&Y=2 type of strings and have the page return an ADO
          > > > > disconnected record set containing the results of the action. This[/color][/color][/color]
          is[color=blue][color=green][color=darkred]
          > > > > call very well, and works, but you do have to allow the browser to
          > > > > create an ADODB.RecordSet in which to place the results which goes
          > > > > against some security principles.
          > > > >
          > > > > Idea 2 was to use an <IFRAME> and replace the src for this frame at
          > > > > run time with the result from an ASP page, again using the ?X=1&Y=2
          > > > > type of string to pass in arguments. The results could then come[/color][/color][/color]
          back[color=blue][color=green][color=darkred]
          > > > > as a table and be accessed via the DOM. The problem with this is[/color][/color][/color]
          that[color=blue][color=green][color=darkred]
          > > > > the client needs to wait for the page to be loaded before it can be
          > > > > sure that the contents of the table is valid and the browser does[/color][/color][/color]
          not[color=blue][color=green][color=darkred]
          > > > > seem to allow waits and does not like busy waits.
          > > > >
          > > > > Idea 3 - I'm still looking for this ! Does anyone have any ?
          > > > >
          > > > > Many thanks
          > > > >
          > > > > Martin
          > > >
          > > > If I understand correctly what you're after, then the answer is[/color][/color][/color]
          XMLHTTP.[color=blue][color=green][color=darkred]
          > > > Google should supply the rest ...
          > > >
          > > > Bob Barrows
          > > > --
          > > > Microsoft MVP -- ASP/ASP.NET
          > > > Please reply to the newsgroup. The email account listed in my From
          > > > header is my spam trap, so I don't check it very often. You will get a
          > > > quicker response by posting to the newsgroup.
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • goinoutwest

            #6
            Re: ASP Page as Remote Function call from Javascript

            "Martin Waller" wrote:[color=blue]
            > I've been playing with the idea of just how to use an ASP page to provide[/color]
            a[color=blue]
            > remote function call. In an ideal world this would be a web service but[/color]
            how[color=blue]
            > can you do it if restricted to ASP 3.0 ?
            > Idea 3 - I'm still looking for this ! Does anyone have any ?[/color]

            I'm making some assumptions here, but I think: you want the browser to get
            some data that the server has. You want the browser to make a client-side
            call to get this data without the current page being reloaded. And you
            would
            like this to be reasonably cross-browser compliant, and not require any
            funky
            client objects (ActiveXObjects , Object tags, XMLHTTP, etc).

            If the above is correct, then this cool trick might work for you.
            Put this function on your "main" page:

            function getNewJSFile(qu erystring) {
            var s = document.create Element("script ");
            s.setAttribute( "src", "http://you.com/ReturnsJavascri ptArray.asp?" +
            querystring);
            s.setAttribute( "type", "text/javascript");
            document.getEle mentsByTagName( "head")[0].appendChild(s) ;
            }

            That function above is ordinary JavaScript which works in almost all
            browsers. (No Netscape 4 or earlier) Now, you can add calls to
            getNewJSFile() on your "main" page like this:

            <a href="javascrip t:getNewJSFile( 'yr=1999&data=s ales')">Show 1999
            Sales</a>
            <a href="javascrip t:getNewJSFile( 'yr=2000&data=s ales')">Show 2000
            Sales</a>

            When a user clicks these links, a new Javascript file will be downloaded
            by the client. But, the file is actually an ASP3.0 page
            (ReturnsJavascr iptArray.asp)
            which returns Javascript. This allows you to query a database and format
            the
            response as a Javascript array, like this:

            Sales_1999(1, 'Jan', '$234.56');
            Sales_1999(2, 'Feb', '$123.45');
            Sales_1999(3, 'Mar', '$456.78');

            The response can even include functions and function calls:

            function show_Sales_1999 () {
            ...code to loop thru array and alert() the sales data
            }
            show_Sales_1999 ()

            Good Luck!
            -Rob


            Comment

            • Martin Waller

              #7
              Re: ASP Page as Remote Function call from Javascript

              Rob,

              Nice one !

              It seems to work but the one thing I'm not sure about is wether the
              appendChild() operation is synchronous or not ? Do you have any idea ?

              Many thanks...

              Martin

              "goinoutwes t" <goinoutwest@ho tmail.com> wrote in message
              news:uGhG84bpEH A.2636@TK2MSFTN GP09.phx.gbl...[color=blue]
              > "Martin Waller" wrote:[color=green]
              > > I've been playing with the idea of just how to use an ASP page to[/color][/color]
              provide[color=blue]
              > a[color=green]
              > > remote function call. In an ideal world this would be a web service but[/color]
              > how[color=green]
              > > can you do it if restricted to ASP 3.0 ?
              > > Idea 3 - I'm still looking for this ! Does anyone have any ?[/color]
              >
              > I'm making some assumptions here, but I think: you want the browser to get
              > some data that the server has. You want the browser to make a client-side
              > call to get this data without the current page being reloaded. And you
              > would
              > like this to be reasonably cross-browser compliant, and not require any
              > funky
              > client objects (ActiveXObjects , Object tags, XMLHTTP, etc).
              >
              > If the above is correct, then this cool trick might work for you.
              > Put this function on your "main" page:
              >
              > function getNewJSFile(qu erystring) {
              > var s = document.create Element("script ");
              > s.setAttribute( "src", "http://you.com/ReturnsJavascri ptArray.asp?" +
              > querystring);
              > s.setAttribute( "type", "text/javascript");
              > document.getEle mentsByTagName( "head")[0].appendChild(s) ;
              > }
              >
              > That function above is ordinary JavaScript which works in almost all
              > browsers. (No Netscape 4 or earlier) Now, you can add calls to
              > getNewJSFile() on your "main" page like this:
              >
              > <a href="javascrip t:getNewJSFile( 'yr=1999&data=s ales')">Show 1999
              > Sales</a>
              > <a href="javascrip t:getNewJSFile( 'yr=2000&data=s ales')">Show 2000
              > Sales</a>
              >
              > When a user clicks these links, a new Javascript file will be downloaded
              > by the client. But, the file is actually an ASP3.0 page
              > (ReturnsJavascr iptArray.asp)
              > which returns Javascript. This allows you to query a database and format
              > the
              > response as a Javascript array, like this:
              >
              > Sales_1999(1, 'Jan', '$234.56');
              > Sales_1999(2, 'Feb', '$123.45');
              > Sales_1999(3, 'Mar', '$456.78');
              >
              > The response can even include functions and function calls:
              >
              > function show_Sales_1999 () {
              > ...code to loop thru array and alert() the sales data
              > }
              > show_Sales_1999 ()
              >
              > Good Luck!
              > -Rob
              >
              >[/color]


              Comment

              • goinoutwest

                #8
                Re: ASP Page as Remote Function call from Javascript

                "Martin Waller" wrote:[color=blue]
                > Rob,
                >
                > Nice one !
                >
                > It seems to work but the one thing I'm not sure about is wether the
                > appendChild() operation is synchronous or not ? Do you have any idea ?
                >
                > Many thanks...
                >
                > Martin
                >[/color]

                appendChild() is synchronous. But, I don't think when you use it to append
                a new script block to the HEAD of a webpage that the code execution stops
                and waits for the browser to "see" this new script block and finish
                downloading
                the file referenced as the SRC of the script block. (did that make sense?)

                I have not tested this, but it should be easy to confirm.
                -r


                Comment

                • Martin Waller

                  #9
                  Re: ASP Page as Remote Function call from Javascript

                  Rob,

                  I tried it and as you say the execution does not stop so the loading of the
                  script is happening after I'm testing for the results so I can't really use
                  that technique !

                  Many thanks for trying...

                  Martin

                  "goinoutwes t" <goinoutwest@ho tmail.com> wrote in message
                  news:#x6LlEypEH A.536@TK2MSFTNG P11.phx.gbl...[color=blue]
                  > "Martin Waller" wrote:[color=green]
                  > > Rob,
                  > >
                  > > Nice one !
                  > >
                  > > It seems to work but the one thing I'm not sure about is wether the
                  > > appendChild() operation is synchronous or not ? Do you have any idea ?
                  > >
                  > > Many thanks...
                  > >
                  > > Martin
                  > >[/color]
                  >
                  > appendChild() is synchronous. But, I don't think when you use it to[/color]
                  append[color=blue]
                  > a new script block to the HEAD of a webpage that the code execution stops
                  > and waits for the browser to "see" this new script block and finish
                  > downloading
                  > the file referenced as the SRC of the script block. (did that make[/color]
                  sense?)[color=blue]
                  >
                  > I have not tested this, but it should be easy to confirm.
                  > -r
                  >
                  >[/color]


                  Comment

                  • goinoutwest

                    #10
                    Re: ASP Page as Remote Function call from Javascript

                    "Martin Waller" wrote:[color=blue]
                    > I tried it and as you say the execution does not stop so the loading of[/color]
                    the[color=blue]
                    > script is happening after I'm testing for the results so I can't really[/color]
                    use[color=blue]
                    > that technique ![/color]

                    That's part of the trick. You probably have some function which handles
                    the task of "testing for results", right?

                    You need to put the call to that function as the last line of that newly
                    downloaded Javascript file.
                    For example, here is a simplified version of the "main" page:
                    <html>
                    <a href="javascrip t:getNewJSFile( 'data=sales')"> Show Sales</a>
                    <script>
                    function getNewJSFile(qu erystring) {...}
                    function handleNewData(a rrayName) {...}
                    </script>
                    </html>

                    And here's a simplified version of that new javascript file that will be
                    downloaded when that link is clicked:
                    /* begin code */
                    SalesData(1, '$234.56');
                    SalesData(2, '$123.45');
                    SalesData(3, '$456.78');
                    handleNewData(' SalesData');
                    /* end code */

                    So, you can see that the data will arrive just before the call to the
                    function
                    which is designed to handle that data. It will always work! My guess is
                    that you were doing something equivalent to this:
                    <a
                    href="javascrip t:getNewJSFile( 'data=sales');h andleNewData('S alesData');">Sh o
                    w Sales</a>
                    In that case, the call to handle the data is going to happen before the data
                    arrives.
                    That's bad.

                    -Rob


                    Comment

                    Working...