混合脚本语言时类未定义错误

发布于 2025-01-05 22:40:39 字数 1256 浏览 0 评论 0原文

以下代码:

<%
Response.Write("VB comes second!")

'page1()
Dim p
Set p = New Page
%>

<script language = "Python" runat="server">
Response.Write("Python comes first!")

class Page:
    def display_top(self):
        Response.Write("""
        <html>
        """)

    def display_body(self, body_text):
        Response.Write("<body>"+body_text+"</body>")

    def display_bottom(self):
        Response.Write("""
        </html>
        """)

def page1():
    p = Page()
    p.display_top()
    p.display_body("This is my body!")
    p.display_bottom()
</script>

给出错误:

Error Type:
Microsoft VBScript runtime (0x800A01FA)
Class not defined: 'Page'
/website/index.asp, line 6

但为什么呢?

如果我从 VBScript 中调用函数 page1(),那么它会按预期工作。

谢谢,

巴里

编辑1:

这:

<script language = "Python" runat="server">
class Page:
    def display_top(self):
        Response.Write("<html>")
</script>

<%
Dim p
Set p = server.createobject("Page")
%>

给出错误:

无效的类字符串

如果我使用:

Set p = New Page

而不是,那么我得到:

类未定义:'Page'

The following code:

<%
Response.Write("VB comes second!")

'page1()
Dim p
Set p = New Page
%>

<script language = "Python" runat="server">
Response.Write("Python comes first!")

class Page:
    def display_top(self):
        Response.Write("""
        <html>
        """)

    def display_body(self, body_text):
        Response.Write("<body>"+body_text+"</body>")

    def display_bottom(self):
        Response.Write("""
        </html>
        """)

def page1():
    p = Page()
    p.display_top()
    p.display_body("This is my body!")
    p.display_bottom()
</script>

Gives the error:

Error Type:
Microsoft VBScript runtime (0x800A01FA)
Class not defined: 'Page'
/website/index.asp, line 6

But why?

If I call the function page1() from within the VBScript, then it works as expected.

Thanks,

Barry

EDIT 1:

This:

<script language = "Python" runat="server">
class Page:
    def display_top(self):
        Response.Write("<html>")
</script>

<%
Dim p
Set p = server.createobject("Page")
%>

Gives the error:

Invalid class string

If I use:

Set p = New Page

instead, then I get:

Class not defined: 'Page'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2025-01-12 22:40:39

我找不到这方面的明确来源,但我很确定 VBScript New 关键字只能用于实例化使用 Class 关键字在 VBScript 中定义的类。相反,您应该做的是在 Python 中编写一个“工厂”函数,该函数将返回一个新的 Page 对象。然后您可以从 VBScript 调用该函数。

我不懂 Python,但这里有一个 VBScript 和 JScript 的示例来说明这一点:

<%
Dim p
Set p = makePage("hello")
Response.Write p.foo
%>

<script language="JScript" runat="server">
function Page(foo) {
    this.foo = foo;
}

function makePage(foo) {
    return new Page(foo);
}
</script>

I can't find a definitive source for this, but I'm pretty sure that the VBScript New keyword can only be used to instantiate classes defined in VBScript using the Class keyword. What you should do instead is write a "factory" function in Python that will return a new Page object. Then you can call that function from VBScript.

I don't know Python, but here's an example with VBScript and JScript to illustrate the point:

<%
Dim p
Set p = makePage("hello")
Response.Write p.foo
%>

<script language="JScript" runat="server">
function Page(foo) {
    this.foo = foo;
}

function makePage(foo) {
    return new Page(foo);
}
</script>
软糯酥胸 2025-01-12 22:40:39

您遇到了 asp 引擎的限制。

服务器脚本执行顺序

内联服务器脚本从上到下按顺序运行。您可以在服务器脚本中定义可调用例程(函数或子例程),它们将根据需要被调用。

所有内联脚本必须使用相同的语言,即页面顶部的 @ 指令中指定的语言。因此,不能在内联脚本中混合使用脚本语言。

“但是等等!”你可能会说。理论上可以将类似内联的脚本放入

<% Response.Write("Some inline script<BR>")%>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
    Response.Write("Script in a SCRIPT element<BR>")
</SCRIPT>

是的,你可以做到这一点。 *但是,您将受到 IIS ASP 处理器的执行顺序的支配。*

脚本块排序

当您混合语言时,

<SCRIPT LANGUAGE="VBScript">
    ' Calls a JScript function
    aNumber = 2
    doubledNumber = doubleMe(aNumber)
    document.write("The answer is " & doubledNumber)
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
    function doubleMe(aNumber){
        return aNumber * 2;
    }
</SCRIPT>

这是行不通的。更具体地说,document.write 语句将向页面写入一个空字符串。为什么?因为在处理 VBScript 块时,以下 JScript

在这种情况下,只需颠倒脚本块的顺序即可解决问题。事实上,这种类型的场景并不常见,在大多数情况下,

要了解更多信息,请访问这篇 MSDN 知识库文章

You are hitting a asp engine limitation right there.

Code in <script runat=server> blocks may run at different times.

Server Script Order of Execution

Inline server script runs sequentially, top to bottom. You can define callable routines (functions or subroutines) in server script, and they will be called as needed.

All inline script has to be in the same language namely, the language specified in the @ directive at the top of the page. Therefore, you can't mix scripting languages in inline script.

"But wait!" you might say. It is theoretically possible to put inline-like script into a <SCRIPT> element that is, have script in the element that is not part of a function or subroutine, as in the following example:

<% Response.Write("Some inline script<BR>")%>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
    Response.Write("Script in a SCRIPT element<BR>")
</SCRIPT>

Yes, you can do this. *However, you are then at the mercy of the order of execution of the IIS ASP processor.*

Ordering Script Blocks

When you are mixing languages, the order in which <SCRIPT> blocks appear in the page can make a difference as to whether they work properly. Consider this simple case of an inline VBScript script calling a function written in JScript:

<SCRIPT LANGUAGE="VBScript">
    ' Calls a JScript function
    aNumber = 2
    doubledNumber = doubleMe(aNumber)
    document.write("The answer is " & doubledNumber)
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
    function doubleMe(aNumber){
        return aNumber * 2;
    }
</SCRIPT>

This won't work. More specifically, the document.write statement will write an empty string to the page. Why? Because at the time the VBScript block is being processed, the following JScript <SCRIPT> block has not yet been read, parsed, and made available to the page. When the browser processes the script blocks in the page, it works from top to bottom.

In this case, simply reversing the order of the script blocks solves the problem. And in fact, this type of scenario is not all that common—for the most part, <SCRIPT> blocks contain functions and subroutines that will not be called until the page has been fully loaded and all elements are available. Nonetheless, you should keep in the back of your mind the fact that pages are processed linearly and that <SCRIPT> blocks in different languages are processed separately.

To learn more, pls visit this MSDN knowledge base article.

怪异←思 2025-01-12 22:40:39
Proof that the class defined in one script block is reachable from another script block; 

<%
Response.Write "VBScript Here - 1 <p>"
hello()

apple.color = "reddish"
response.write (apple.getInfo())

%>

<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 2 <p>");
    function hello()
    {
        Response.Write("Hello from JS <p>");
    }


    var apple = {
        type: "macintosh",
        color: "red",
        getInfo: function () {
            return this.color + ' ' + this.type + ' apple' + '<p>';
        }
    }


</script>

<%
    Response.Write("VBScript here - 3 <p>")
%>


<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 4 <p>");  
</script>

<%
    Response.Write("VBScript here - 5 <p>")
%>


will give you this 
Javascript here - 2
Javascript here - 4
VBScript Here - 1
Hello from JS
reddish macintosh apple
VBScript here - 3
VBScript here - 5
Proof that the class defined in one script block is reachable from another script block; 

<%
Response.Write "VBScript Here - 1 <p>"
hello()

apple.color = "reddish"
response.write (apple.getInfo())

%>

<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 2 <p>");
    function hello()
    {
        Response.Write("Hello from JS <p>");
    }


    var apple = {
        type: "macintosh",
        color: "red",
        getInfo: function () {
            return this.color + ' ' + this.type + ' apple' + '<p>';
        }
    }


</script>

<%
    Response.Write("VBScript here - 3 <p>")
%>


<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 4 <p>");  
</script>

<%
    Response.Write("VBScript here - 5 <p>")
%>


will give you this 
Javascript here - 2
Javascript here - 4
VBScript Here - 1
Hello from JS
reddish macintosh apple
VBScript here - 3
VBScript here - 5
疏忽 2025-01-12 22:40:39

您需要颠倒脚本顺序。当您的脚本遇到函数调用时,它允许该函数块位于页面上同一范围内的任何代码块中。实例化对象时情况并非如此。实例化对象时,必须在实例化行之前处理对象代码。脚本的所有意图和目的都是线性执行的。

You need to reverse the script order. When your script encounters a function call, it allows that function block to be in any code block on the page within the same scope. This is not true when instantiating objects. While instantiating objects, the object code must be processed before the instantiating line. Scripting is for all intents and purposes executed linearly.

浪漫之都 2025-01-12 22:40:39

问题在于执行顺序。我的服务器上没有安装 python。所以我用 JS 和 VB 为你做了一个测试 - 这和我提到的问题是一样的。看看吧;

<%
Response.Write "VBScript Here - 1 <p>"
%>

<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 2 <p>");
</script>

<%
    Response.Write("VBScript here - 3 <p>")
%>


<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 4 <p>");  
</script>

<%
    Response.Write("VBScript here - 5 <p>")
%>

will give you this

Javascript here - 2
Javascript here - 4
VBScript Here - 1
VBScript here - 3
VBScript here - 5

The problem is the order of execution. I don't have python installed on my server. So I did a test for you with JS vs VB - which is the same problem I referred to. Take a look;

<%
Response.Write "VBScript Here - 1 <p>"
%>

<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 2 <p>");
</script>

<%
    Response.Write("VBScript here - 3 <p>")
%>


<script language=JavaScript runat=Server>
    Response.Write("Javascript here - 4 <p>");  
</script>

<%
    Response.Write("VBScript here - 5 <p>")
%>

will give you this

Javascript here - 2
Javascript here - 4
VBScript Here - 1
VBScript here - 3
VBScript here - 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文