473,396 Members | 1,707 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,396 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 2417


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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.