Uncaught ReferenceError: is not defined

Uncaught ReferenceError: is not defined

我有一个 javascript 函数可以发出 ajax get 请求。这是一个遗留应用程序,我能做的有限。我不能使用 jquery。

如您所见,我正在使用 scriptlet 访问请求范围内的对象。我正在尝试提醒 scriptlet 返回的值,但我一直收到

Uncaught ReferenceError: google is not defined

"google" 是 scriptlet 返回的值,即 expected/correct 值。

function getLinkAddress(linkClicked){
    httpGetUrl("http://www.google.com");    
    alert(<%=((DynaActionForm) request.getAttribute("ipacForm")).get("url").toString() %>);
}       

function httpGetUrl(theUrl){
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", "?screenName=LinkAddress&buttonName=get", false);
            xmlHttp.send(null);
        }

正如您在我的调试控制台下图中所见,alert() 中的值正确计算为 "google.com"

您正试图警告一个字符串,但您的代码认为它是一个变量,因此出现 "not defined" 错误。您需要用引号将您的答案括起来:

alert("google.com");

问题是 window.alert 方法需要一个字符串,而您正在为它传递一个未定义的值。浏览器会尝试找到一个全局变量 google 和一个 属性 到名为 comgoogle 中,所以它不会找到,并且会抛出错误。

你要的是alert一个字符串'google.com',所以需要引号:

alert('<%=((DynaActionForm) request.getAttribute("ipacForm")).get("url").toString() %>');