我在Classic ASP上遇到一些Session变量的麻烦。 在以下代码的第19行,我收到“类型不匹配:'我'”错误
<% '---------------------------------------------------------------------------- ' File: /include/script/debug.asp ' Author: Vladimir Charkot ' Create Date: 15/05/2013 ' Description: Generate a server debug log on client '---------------------------------------------------------------------------- Redim debugTable(2,0) Sub initDebug(debugLevel) Session("debugEntries") = 0 Session("debugLevel") = "e" Call debugMsg("e","Debug initialized") End Sub Sub debugMsg(lv, str) If IsEmpty(Session("debugEntries")) Then i = 0 Else i = Session("debugEntries") <-- Line 21, Type mismatch error IF CInt() IS APPLIED TO SESSION VARIABLE End If i = i + 1 <-- Line 23, Type mismatch error Redim Preserve debugTable(2,i) debugTable(0,i-1) = lv debugTable(1,i-1) = str Session("debugEntries") = CInt(i) End Sub Function debugToClient() Dim list Dim func list = jsDebugList() func = jsDebugFunction(list) debugToClient = func End Function Function jsDebugList() Dim i Dim list If IsEmpty(Session("debugEntries")) Then i = 0 Else i = Session("debugEntries") End If list = "[" for n = 0 to i - 2 'Add the last one outside the loop list = list & "{debugLevel : """ & debugTable(0,n) & """, message : """ & debugTable(1,n) & """}, " ' i.e.: {debugLevel : "e", message : "Error on application"}, next list = list & "{debugLevel : """ & debugTable(0,n) & """, message : """ & debugTable(1,n) & """}" list = list & "]" jsDebugList = list End Function Function jsDebugFunction(l) Dim f f = "function debug(){" f = f & " debugList = " & l & ";" f = f & " for (elem in debugList){" f = f & " console.log(elem.debugLevel + "": "" + elem.message);" f = f & " }" f = f & "}" jsDebugFunction = f End Function %>
<!-- #include virtual="/gu/include/script/debug.asp" --> sub InitSession() (...stuff...) Call initDebug("e") end sub sub CheckSession() ' If not a new session, this code has already been executed. if stuff then timeout() end if Call debugMsg("e", "CheckSession() Executed") end sub
希望你能帮助我,我真的认为我已经尝试了一切。除了巫毒,黑魔法等等。 谢谢!
答案 0 :(得分:2)
在调用debugMsg()
方法之前调用initDebug()
方法,导致Session("debugEntries")
为空,结果只是一个空字符串。
尝试将字符串转换为整数时,确实会出现“类型不匹配”错误,请注意i = i + 1
会尝试自动转换。
要解决此问题,请确保始终先致电initDebug()
或添加“failafe”代码:
If Session("debugEntries")="" Then
i = 0
Else
i = CInt(Session("debugEntries"))
End If
i = i + 1
答案 1 :(得分:1)
会话“数字”实际上是字符串。如果要使用整数:
i = CInt(Session("debugEntries"))