views:

58

answers:

2

After I execute this code asp scripts quite working.

<!-- #include file="Connection.asp" -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<%
function Page()
{   
    var db = new DBConnection;
    this.DAL = db.retriveDAL("Content");

    var url = new String(Request.ServerVariables("QUERY_STRING")), site = new String(Request.ServerVariables("QUERY_STRING"));
    site = url.slice(4, url.indexOf(":80", 0)) + "/";
    url = url.slice(url.indexOf("80", 0) + 2, url.length).split("/");

    var pageName = url[1], pageID = url[2];

    var xmlhttp = Server.CreateObject("Microsoft.XMLHTTP");

    xmlhttp.open("POST", site+"library/Datastore.asp?page="+pageName + (pageID ? "id=" + pageID : ""), false);
    xmlhttp.send();

    var xml = Server.CreateObject("Microsoft.XMLDOM");
    xml.async = false;

    xml.loadXML(xmlhttp.responseText);

    var xsl = Server.CreateObject("Microsoft.XMLDOM");
    xsl.async = false;
    xsl.load(Server.MapPath("templates/" + pageName + ".xsl"));

    Response.Write(xml.transformNode(xsl));
}
%>

Does anybody know why? There is no reason for it now to work. It also disables any asp script in the website.

EDIT: I just disabled the xmlhttp and re-created the sites and asp works fine.
EDIT 2:
I just discovered that the Server.Transfer is causing the endless runtime.
Any idea why?

A: 

Are you calling the script off the same server? If so you will have problem with Session being locked. When a page has Session it locks the Session object for that user until it finishes (or timesout). When you call the second page on the same server it hangs while it waits for the Session lock to be freed (which can't happen as the calling page has it). You end up with the page timing out.

Can be fixed by switching off session for either of the pages.

Pete Duncanson
In order for ASP to associate the XMLHTTP request with the current session the ASP session cookie would need to be deliberately duplicated in the request but that doesn't appear to be happening here.
AnthonyWJones
+4  A: 

Tip #1:

Build a proper URL. You use

site
 + "library/Datastore.asp?page="
 + pageName
 + (pageID ? "id=" + pageID : "")

but correct is

site
  + "library/Datastore.asp?page="
  + Server.URLEncode(pageName) 
  + (pageID ? "&id=" + Server.URLEncode(pageID) : "")
//-------------^  !!

Tip #2:

Instead of

Response.Write(xml.transformNode(xsl));

use

xml.transformNodeToObject(xsl, Response);

This way you won't run into any output encoding issues.

Tip #3: (From the comments: This turned out as wrong.)

Request.ServerVariables("QUERY_STRING")

already is a string. No need to wrap it in new String(). Apparently, strings that come out of Request.ServerVariables are not JS strings, so constructing a native String object is really necessary here.

Tip #4:

For use on a HTTP server (that is multi-threaded by its very nature), you should be using Msxml2.ServerXMLHTTP instead of Microsoft.XMLHTTP and instead of Microsoft.XMLDOM you should be using MSXML2.FreeThreadedDOMDocument.

Tomalak
+1 ServerXMLHTTP should be used, not sure about FreeThreadedDOMDocument, why not simply DOMDocument? BTW transformNodeToObject to Response is now broken in MXSML SP10.
AnthonyWJones
@Anthony: Thanks for the hint regarding MSXML SP10. Can you specify "broken"? DOMDocument would be okay, too, but on a web server these things sometimes end up being cached in the Application object, at which point I would use FreeThreadedDOMDocument. So, it won't hurt to use it right away.
Tomalak
As of SP10 TransformToObject calls the `Commit` method of the `IStream` interface whereas previously it didn't. Unfortunately the ASP Response object implementation of `IStream` throws a "Not Implemented" exception when that method is called.
AnthonyWJones
@Anthony: Thanks for the feedback, good to know. Very lame of MS, too - I guess it would not have been too hard for them to work around that. Lets hope they fix this at some point.
Tomalak
About Tip #3: Apparently it is a string but not a javascript String object. If I try to use String functions on it, it says that the object is undefined or null.
the_drow
@the_drow: You might be right. It could be a Variant of type string, I admit that I assumed that this "boxing" in a JS native String object would happen automatically. I'll change may answer accordingly.
Tomalak