0

I am trying to write a simple .asp file that displays all of the keys and values from Request.ServerVariables, and, somehow, I am not able to find anything within Reuqest.ServerVariables.

This is my test.asp:

<%@ language="Javascript" %>
<!-- #include file="scripts/testjs.asp" -->
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="displayUserStuff()">
<p id="userstuff"></p>
</body>
</html>

This .asp file calls another .asp file that will transfer the list from Request.ServerVariables into a JavaScript string for display, called testjs.asp:

<%
var html = "";

for (var prop in Request.ServerVariables.Keys) {
    Response.Write("X<br>");
    html += prop +  "<br />\n";
}

%>
<script>
function displayUserStuff() {
    document.getElementById('userstuff').innerHTML = '<%= Request.ServerVariables("ALL_RAW") %> <%= html %>';
}
</script>

However, whenever I run test.asp, the page is empty. Nothing apparently occurs in the loop, and I am unsure as to why.

Environment: Windows 10, IIS 8, Classic ASP, and JavaScript

Bharata
  • 13,509
  • 6
  • 36
  • 50
  • The `Request.ServerVariables` collection does not work like a `Scripting.Dictionary` it does not have a `Keys` property. See the duplicate for the correct approach. – user692942 Jul 22 '20 at 05:19
  • 1
    See:[How To Use JScript or JavaScript to Traverse Through a Collection](https://support.microsoft.com/en-in/help/229693/how-to-use-jscript-or-javascript-to-traverse-through-a-collection) – Flakes Jul 22 '20 at 09:04
  • If you check the browser console, you will see a couple of errors, that's why you are not even seeing the output of Request.ServerVariables("ALL_RAW").(if you do a view-source of the page, you can see that). Use encodeURI to escape the output of Request.ServerVariables("ALL_RAW") as: `<%= encodeURI(Request.ServerVariables("ALL_RAW")) %>` – Flakes Jul 22 '20 at 10:52
  • 1
    @Flakes: The browser console did not indicate any errors whenever I used <%= Request.ServerVariables("ALL_RAW") %>; as a matter of fact, I did get syntax errors the moment I tried to use <%= encodeURI(Request.ServerVariables("ALL_RAW") %> in the console instead. However, your advice on the Request.ServerVariables collection worked. Thanks – user1544358 Jul 22 '20 at 11:23
  • Okay. I tried your code in chrome and did get console messages. Anyway, glad it worked for you. – Flakes Jul 22 '20 at 11:25

0 Answers0