473,237 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

POSTing html via XmlHttpRequest

i'm a relative newbie to javascript and the more advanced techniques of Web
dev't so bear with me. what i'd like to accomplish is twofold..
1. in client-side JS in an aspx file, i'd like to POST straight html via the
Request object down to an asp page that runs server-side script.
2. i want this server-side script to retrieve the html from the Request
object, place it into the Response object in such a way that i can save/open
it as an Excel file. i'd prefer to have the script force an Open/Save dialog
to open and prompt me for what i want to do.
this is what i have and i've tried a zillion variations but i just can't get
it to work.. the version below errors out on the doc.save with a message
saying i need a high-level element.

client-side aspx..

var str = document.all.Table2.outerHTML;
alert(str); // i get the html
var req = new ActiveXObject("Microsoft.XMLHTTP");
alert(req); // valid object
var doc = new ActiveXObject("Microsoft.XMLDOM");
alert(doc); // valid object
doc.load(str);
req.open("POST", "http://localhost/testServSideXL/WebForm1.asp", false);
req.send(doc);
return req.responseXML;

server-side asp (WebForm1)..

<%@ Language="javascript" %>
<%
var fileName = "MyReport-Excel Export";
fileName = fileName + ".xls";
Response.AddHeader("Content-disposition", "Attachment; filename=" +
fileName);
Response.ContentType = "application/download";

var doc = Server.CreateObject("Msxml2.DOMDocument");
doc.load(Request);
doc.save(Response);
// var innerHTML = doc.xml;
// Response.Write(Request);
// Response.Write(innerHTML);
%>


Aug 9 '05 #1
2 2413


Slipperman wrote:

1. in client-side JS in an aspx file, i'd like to POST straight html via the
Request object down to an asp page that runs server-side script.
var str = document.all.Table2.outerHTML;
alert(str); // i get the html var doc = new ActiveXObject("Microsoft.XMLDOM");
alert(doc); // valid object
doc.load(str);


You can load well-formed XML with the load method but certainly not what
IE gives you as outerHTML, at least not in most cases.
Most likely if you checked
doc.parseError.errorCode != 0
it would give true and
doc.parseError.reason
would tell you a parse error message.
So loading the outerHTML or innerHTML IE gives you in an HTML text/html
document with Microsoft.XMLDOM is not going to work reliably, if you
want to send HTML markup then you can still post it using
Microsoft.XMLHTTP but you have to send it as a string, presumably an
application/x-www-form-urlencoded one e.g. (IE only code)
var httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
httpRequest.open('POST', 'whatever.asp', true);
httpRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
httpRequest.send('html=' + encodeURIComponent(str));
Then in the ASP page you can read out
Request.Form.Item('html')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 9 '05 #2
thanx Martin..
this actually didn't work. all along with all the different things i've
tried, the only time i was able to get the Open/Save dialog from
WebPage1.asp to display was by typing in its url directly in the addr bar.
but of course, nothing was posted in this situation so Excel errored out
with an 'Unable to read file' message.
this happened with your code too (not blaming you, just think there's
something else going on that i'm not aware of). have any ideas on how i can
debug what's going on on the server end?? i'm realizing now that i don't
know for sure that anything actually happens on the server end after the
POST is sent. since i'm assuming that WebPage1 has to actually load in order
for the dialog appear, maybe there's something i'm not doing that would
cause this to happen at the time the Request is received??

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...


Slipperman wrote:

1. in client-side JS in an aspx file, i'd like to POST straight html via
the
Request object down to an asp page that runs server-side script.


var str = document.all.Table2.outerHTML;
alert(str); // i get the html

var doc = new ActiveXObject("Microsoft.XMLDOM");
alert(doc); // valid object
doc.load(str);


You can load well-formed XML with the load method but certainly not what
IE gives you as outerHTML, at least not in most cases.
Most likely if you checked
doc.parseError.errorCode != 0
it would give true and
doc.parseError.reason
would tell you a parse error message.
So loading the outerHTML or innerHTML IE gives you in an HTML text/html
document with Microsoft.XMLDOM is not going to work reliably, if you want
to send HTML markup then you can still post it using Microsoft.XMLHTTP but
you have to send it as a string, presumably an
application/x-www-form-urlencoded one e.g. (IE only code)
var httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
httpRequest.open('POST', 'whatever.asp', true);
httpRequest.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
httpRequest.send('html=' + encodeURIComponent(str));
Then in the ASP page you can read out
Request.Form.Item('html')

--

Martin Honnen
http://JavaScript.FAQTs.com/

Aug 9 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christian Schmitt | last post by:
Hi, I want to write a script that (run from a file on the local HD) reads another local html-file into a variable, then make some changes to it, and then output the result in a way that makes it...
2
by: alex bazan | last post by:
I've got this piece of code that imports an XML document ... it works for both mozilla and IE. function getXMl(url) { if (!document.all) { var xmlDoc =...
9
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
7
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it...
6
by: mihirnmehta | last post by:
This is my code function getDetails() { var name = document.getElementById("movie_name").value; if (window.XMLHttpRequest) //For Mozilla Browsers { XMLHttp=new XMLHttpRequest() }
3
by: one.1more | last post by:
How do i post the form data to different php files at once. I tried the following code but it doesn't work. the data is sent only to the first php file. <form method="post" action="insert.php"...
5
by: chadschultz | last post by:
Hi there! A friend of mine asked about how to have a line of HTML be written based on whether or not a certain file exists in the server. So if the file is there, a line of HTML is printed; if the...
10
by: J | last post by:
Hi, Ajax question: Can I retrieve an HTML page and use the DOM to grab stuff from the requested page in a similar fashion to an XML page requested in the same manner? Details: I'm requesting...
3
by: vunet.us | last post by:
Hello, I am breaking my head running out of ideas about the best solution to my goal. I want to load some pages generated with the server (ASP) and assign their html results to JavaScript, so...
1
by: Duke | last post by:
<html> <head> <script type="text/javascript"> function init(){ var html = document.open('Hello.html'); document.getElementById } </script> <body onload ="init();"> <did = 'hi'>
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.