By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

POSTing html via XmlHttpRequest

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.