混合脚本语言时类未定义错误
以下代码:
<%
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找不到这方面的明确来源,但我很确定 VBScript
New
关键字只能用于实例化使用Class
关键字在 VBScript 中定义的类。相反,您应该做的是在 Python 中编写一个“工厂”函数,该函数将返回一个新的Page
对象。然后您可以从 VBScript 调用该函数。我不懂 Python,但这里有一个 VBScript 和 JScript 的示例来说明这一点:
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 theClass
keyword. What you should do instead is write a "factory" function in Python that will return a newPage
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:
您遇到了 asp 引擎的限制。
块中的代码可能在不同时间运行。
服务器脚本执行顺序
内联服务器脚本从上到下按顺序运行。您可以在服务器脚本中定义可调用例程(函数或子例程),它们将根据需要被调用。
所有内联脚本必须使用相同的语言,即页面顶部的 @ 指令中指定的语言。因此,不能在内联脚本中混合使用脚本语言。
“但是等等!”你可能会说。理论上可以将类似内联的脚本放入
元素中,即在不属于函数或子例程的元素中包含脚本,如下例所示:
是的,你可以做到这一点。 *但是,您将受到 IIS ASP 处理器的执行顺序的支配。*
脚本块排序
当您混合语言时,
块可能会影响它们是否正常工作。考虑一下内联 VBScript 脚本调用用 JScript 编写的函数的简单情况:
这是行不通的。更具体地说,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: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: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.
您需要颠倒脚本顺序。当您的脚本遇到函数调用时,它允许该函数块位于页面上同一范围内的任何代码块中。实例化对象时情况并非如此。实例化对象时,必须在实例化行之前处理对象代码。脚本的所有意图和目的都是线性执行的。
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.
问题在于执行顺序。我的服务器上没有安装 python。所以我用 JS 和 VB 为你做了一个测试 - 这和我提到的问题是一样的。看看吧;
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;