Request.ServerVariables(“..”) returns undefined when assigned to a variable yet its value can be displayed via Response.Write(“..”)

ε祈祈猫儿з 提交于 2021-01-29 12:22:08

问题


I am trying to retrieve the user's IP address and assign it to a variable:

var ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR") || 
                Request.ServerVariables("REMOTE_ADDR") || 
                Request.ServerVariables("HTTP_HOST"); 

Response.Write(Request.ServerVariables("HTTP_HOST") + "<br />\n\n");    // produces "localhost"
Response.Write(Request.ServerVariables("REMOTE_ADDR") + "<br />\n\n");  // produces "::1"
Response.Write(Request.ServerVariables("HTTP_X_FORWARDED_FOR") + "<br />\n\n"); // produces "undefined"
Response.Write("ipAddress = " + typeof ipAddress + " " + ipAddress + "<br />\n\n");     // produces "ipAddress = object undefined"

I am using JScript for Classic ASP. I am unsure as to what to do at this point. Can anyone help?

Thanks


回答1:


Things work in ASP with JScript a little bit different than ASP with VBScript.

Since everything is an object in JavaScript, with var ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR") you get an object reference instead of a string value because like most of other Request collections, the ServerVariables is a collection of IStringList objects

So, to make that short-circuit evaluation work as you expect, you need to play with the values, not the object references.

You can use the Item method that returns the string value of IStringList object if there's a value (the key exists), otherwise it returns an Empty value that evaluated as undefined in JScript.

var ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR").Item || 
                Request.ServerVariables("REMOTE_ADDR").Item || 
                Request.ServerVariables("HTTP_HOST").Item; 



回答2:


I solved the problem with obtaining the IP address, and the truthyness/falsyness of JScript is a complete and utter nightmare.

if (!String.prototype.isNullOrEmpty) {
        String.isNullOrEmpty = function(value) {
            return (typeof value === 'undefined' || value == null || value.length == undefined || value.length == 0);
        };
}

var ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR") || 
                Request.ServerVariables("REMOTE_ADDR") || 
                Request.ServerVariables("HTTP_HOST"); 

function getIPAddress() {
        try {
            if (String.isNullOrEmpty(ipAddress)) {
                ipAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
            }
            
            if (String.isNullOrEmpty(ipAddress)) {
                ipAddress = Request.ServerVariables("REMOTE_ADDR");
            }
            
            if (String.isNullOrEmpty(ipAddress)) {
                ipAddress = Request.ServerVariables("HTTP_HOST");
            }
        } catch (e) {
            Response.Write("From getIPAddress(): " + e.message);
            hasErrors = true;
        } finally {
            return ipAddress;
        }
}


    ipAddress = getIPAddress();
    Response.Write("ipAddress = " + typeof ipAddress + " " + ipAddress + "<br />\n\n");     // produces "object localhost"


来源:https://stackoverflow.com/questions/63748751/request-servervariables-returns-undefined-when-assigned-to-a-variable-yet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!