• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Pulling POST result data into javascript...

I would like to capture the result of a form POST in a JavaScript.  I have a servlet that can send me just the data blob that is the result of a form POST, but it seems that the result of a form wants to go to a new page.  Is this true?  Is there a way to capture the returned data into a JS???

Thanks,

GregL
0
GregL
Asked:
GregL
  • 6
  • 5
  • 4
  • +1
1 Solution
 
jbirkCommented:
There is no way to return the data directly to the javascript on that page.  It needs to go to a new page.

However, you could create that new page to look exactly like the previous one plus the new data if you wanted it to look the same, or you could have the cgi program return the line:
"Location: http://www.url.com\n\n";
and then to continue the data along, you could make that url the same as the starting page, and add this on the end: "?name=josh&something=something" using name=value pairs with the '&' delimiting them.  You also have to make sure not to use any characters not allowed like spaces.  This is called url encoding.

Then in the page you can check via javascript what is in the location, and parse out that data from the url and use it.

Is this what you are looking for?
-Josh

0
 
GregLAuthor Commented:
jbirk- Thanks... I will reopen this to see if I can get what I hope is true... If not then I will give you the points...
0
 
jbirkCommented:
The method I suggested could be tweaked to work for many applications.  Do you have access to the servlet that you mentioned in order to implement this?  Otherwise, it will be close to impossible unless the "data blob" can be generated in javaScript forgoing the need for the servlet.
-Josh
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KevJSLCommented:
I am a newbie to JavaScript, however my initial solution (a very sloppy one unfortunately) to reduce the amount of data sent by the server and save you the bother of processing that 'orrible Querystring (?..&..+.), is to pass back a hidden form to a frame (arghh frames!) in that way you can access each parameter directly (much faster method) using something like:

top.frames['<HiddenFrameName>'].<FormName>.<ParameterName>.Value

In this way you don't have to send a page back looking like the one you just actioned.

Make sense?

0
 
jbirkCommented:
KevJSL: Yes, sounds like a nice solution, but when you submit a form in one frame how to you cause it to load the results into another frame??
Or are you saying to have the results posted onto a new page in the same frame, and have another frame access those variables through javascript?
-Josh
0
 
GregLAuthor Commented:
Actually, I am writing both sides.  I have the option to do whatever.  What I am after is a way to have a portion of the page sent to me by a servlet.
0
 
jbirkCommented:
In my opinion as much CGI code as you can avoid the better.  JavaScript is so much easier to code, and to debug!  So I like KevJSL's solution better, although it will require a little bit of modification to the servlet as well.
I thought about it and think I now more fully understand the best way to do it.  Have the original page load with an invisible frame which contains an invisible form.  Then when the user fills out the form on the visible frame and chooses submit, have it copy the values over to hidden form elements in the hidden frame and submit that frame.  Then have the servlet write out a new html page which contains nothing but a hidden form, which contains all of the values needed initialized in the hidden form elements.  Then perhaps use an onLoad command in the new form to call a javascript function on the visible frame which will then evaluate the data in the hidden frame, and do your stuff!  It sounds like a workable solution!!

Tell me if you see this as possible for you, and where you might need some help.
-Josh
0
 
QuixoteCommented:
sybe and I were working on a similar concept, Josh.  I think the hidden frame idea is sound, but you don't need that much inter-frame form communication.  You can set a target to a form, and have the results appear in another window or frame.

Here's some code.  Page 1, the frameset:


<script>
<!-- conceal
var HiddenFrameReady = 0;
// reveal -->
</script>

<frameset rows="100%,*">
   <frame name="content" src="content.html">
   <frame name="hidden"  src="blank.html">
</frameset>



Page 2, "hidden":

<body onUnload="top.HiddenFrameReady = 0;">



Page 3, "content":

<script>
<!-- conceal

function getBlob()
   {
   if (top.HiddenFrameReady)
      {
      var Blob = top.hidden.document.Blob.Output.value;

      // all your post-POST code here
      // during debugging, use alert(Blob) just to make sure you got it
      }
   else
      setTimeout('getBlob()',500);
   }

// reveal -->
</script>





<form method="post"
    action="servlet.cgi"
    target="hidden"
    onSubmit="getBlob();">



</form>




and (if your servelet can return an HTML page) your servelet returns the blob in this fashion:

<html>
<body onLoad="top.HiddenFrameReady = 1"
     onUnload="top.HiddenFrameReady = 0;">

<form name="Blob">
   <textarea name="Output">

{servelet Blob here}

   </textarea>
</form>
</body>
</html>



Hope that gets you there, Greg.  If so, I'll repost as the answer.
0
 
jbirkCommented:
Quixote: That's some nice refinement of the idea!  Did you guys work on that via EE, or do you know each other?
-Josh

P.S. I love the comment you use for the beginning and ending of scripts!  That's cool.
<script>
<!-- conceal
// reveal -->
</script>
0
 
QuixoteCommented:
Josh-

sybe mentioned using an invisible frame with a meta- or JS-refresh timer that will constantly pull new server-side data into the browser window, making fresh data available to other frames.  Mimics push from the user's standpoint.  We refined the idea and I'm probably going to fire-test it later this month.  The concept came up in EE discussion.  I don't know if the refresh is applicable to GregL's problem, but the problems are similar: how does a static page on the browser grab additional data from the server?


In regard to the comments:  I learned JavaScript and HTML by reading other's code.  I always felt that code should be more than legible -- entertaining if possible.  And I noticed the commenting-out of script to hide from incapable browsers has started to fall out of vogue.  I thought it couldn't hurt to spruce it up with something catchy.  In development at my vanity site is a section on JavaScript;  I entitle it "conceal//reveal".  I look forward to finishing it now that I hear your comments...

Thanks.
0
 
GregLAuthor Commented:
Actually, I'm not using CGI. I'm using java in a servlet (JSDK 2.0), but that really doesn't matter.  I was looking for a JS way to get just data from a servlet so that I could format the data on the client, not the server.  This is easy if you use a CORBA-Applet bridge, but I didn't want something that heavy.  Quixote - will get the points if he responds with an answer..


0
 
QuixoteCommented:
GregL-

I have reposted the answer as is, and added an addendum to the end which I think may help you out in looking for a pure "get the data only" solution:



You can set a target to a form, and have the results appear in another window or frame.

Here's some code.  Page 1, the frameset:


<script>
<!-- conceal
var HiddenFrameReady = 0;
// reveal -->
</script>

<frameset rows="100%,*">
   <frame name="content" src="content.html">
   <frame name="hidden"  src="blank.html">
</frameset>



Page 2, "hidden":

<body onUnload="top.HiddenFrameReady = 0;">



Page 3, "content":

<script>
<!-- conceal

function getBlob()
   {
   if (top.HiddenFrameReady)
      {
      var Blob = top.hidden.document.Blob.Output.value;

      // all your post-POST code here
      // during debugging, use alert(Blob) just to make sure you got it
      }
   else
      setTimeout('getBlob()',500);
   }

// reveal -->
</script>

***

<form method="post"
      action="servlet.cgi"
      target="hidden"
      onSubmit="getBlob();">

***

</form>




and (if your servelet can return an HTML page) your servelet returns the blob in this fashion:

<html>
<body onLoad="top.HiddenFrameReady = 1"
      onUnload="top.HiddenFrameReady = 0;">

<form name="Blob">
   <textarea name="Output">

{servelet Blob here}

   </textarea>
</form>
</body>
</html>


***addendum***
however, if you want the javascript to retrieve the data from a pure data file, then there is another method if you meet the follwing requirements:
   1) Don't mind Netscape 2 and partial IE3 incompatibility
   2) Can format the data as javascript variables in a plain text file on the server, with the extension of ".js"

If so, then you can retrieve the data by having the servlet send a redirect to the following HTML:


<html>

<script src="Blob.js"></script>

<body onLoad="top.HiddenFrameReady = 1"
      onUnload="top.HiddenFrameReady = 0;">

[intentionally blank]
 
</body>
</html>
0
 
KevJSLCommented:
I have been thinking a little more about the possiblility of bypassing my original hidden frame/form idea and am proposing a question to you all.

As you will know, it is possible to send different MIME types from the Servlet another than 'text/html', for example when creating visitor counter gifs, etc. This leads me to deviate from the current thinking onto ways of possible processing raw binary/textal data passed by the Servlet using JavaScript.

(Line of thinking, it is possible to recieve image data on-the-fly at any part of a page from the server using the tag <img src="CGIProgramName.cgi">, so are there ways of receiving this sort of data for processing, possibly without using frames and forms?)

Any comments of, 'your mad to even be considering that' etc. will be taken to heart.

P.S. Please vist my final year university dissertation regarding Database/Web interfacing (HTML-Forms-CGI-ODBC-ISAPI-ActiveX-ASP-ADO) at 'http://www.hipstream.force9.co.uk' you might find some useful stuff there.

Cheers,

Kev.
0
 
QuixoteCommented:
Kev-

Brilliant concept, but the Image object cannot be parsed using javascript.  Perhaps you could use javascript to call the image data, pass the data to Java, which could tear it apart and send it back to JavaScript...

Fascinating, but I don't know enough Java to do anything like that.

Anyone?
0
 
KevJSLCommented:
I have spent some time investigating and conclude that if you only have access to JavaScript, the hidden frame option (<frameset cols=100%,*>) is probably the least sloppy solution to the problem described above.

However if you move away from just using JavaScript and write a Java applet you can easily use LiveConnect to communicate with JavaScript and get the java applet to process the data blob.

By the way, don't forget to visit http://www.hipstream.force9.co.uk

final answer, use LiveConnect....(Do I win any points?)
0
 
jbirkCommented:
Except LiveConnect is Netscape only...
-Josh
0
 
GregLAuthor Commented:
I succumb to the prospect that there is no 'good' way to do what I am suggesting.  I am using a servlet to serve up all of my code, backgrounds, scripts, stylesheets and pages.  It works really well.  I was hoping for an elegant solution to page data.  I can use an applet, but since I need SSL, it is too much trouble to code a secure connection. (I'm lazy)  It also makes the page to slow.  Client side java is still not quite there in terms of speed.  Thanks to all who helped. I wish I could give all of you some points.

GregL
0
 
KevJSLCommented:
I'll fight Quixote for the points. (Kings Cross Station London, Platform 2, 5pm, Friday)
0
 
QuixoteCommented:
I'll be there!  And you better... wait a minute.  Did you say London?  I'm, uh, busy that day.  Laundry.  No clean socks.  Gotta have clean socks.

I think I hear Sancho calling...  (Kev- check out the main topic area...)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now