不在asp经典中捕获存储过程中的返回值

时间:2015-01-20 18:22:05

标签: javascript sql-server ajax sql-server-2008 asp-classic

很抱歉这篇长篇文章,但我想要包含所有细节。

我有一个使用ajax的JavaScript函数,它调用一些asp代码,后者又调用存储过程来检查记录是否已经存在。如果确实存在,则会显示一个图像,如果不存在则会显示另一个图像。由于某种原因,我无法在我的asp变量中捕获结果,它返回空,这使得调用始终返回false。我已经自己测试了SQL过程,它可以工作,因此数据就在那里。这是可以更好地解释的代码。

存储过程

ALTER PROCEDURE [dbo].[UserName_Get] 
    -- Add the parameters for the stored procedure here
    @UserName nvarchar(50)
    ,@FacilityID INT
    ,@UserNameExists INT OUT
AS

BEGIN
    SELECT @UserNameExists= ISNULL((SELECT UserID FROM MyTable WHERE FacilityID = @FacilityID AND Username = @UserName),0)

END

经典ASP代码:

<% Response.Buffer = True %>
<!-- #include file="../../_app_init.asp" -->

<%
Dim action, p_UserName
action = SqlSanitizeStr(trim(request("action")))
'dim UserNameExists

If action = "CheckUserName" Then
    ' Check to see if this username already exists for this facility
    ' This is an Ajax routine to check if the username already exists at the current facility or not. Returns "False" if UserName does not exist.

    p_UserName=SqlSanitizeStr(trim(request("UserName")))


    Set objCmd = Server.CreateObject("ADODB.Command")
    Set objCmd.ActiveConnection = DB_CONN
    objCmd.CommandText = "UserName_Get"
    objCmd.CommandType = adCmdStoredProc
    objCmd.Parameters.Append objCmd.CreateParameter("@UserName",        adLongVarChar,      adParamInput,   50        , p_UserName)
    objCmd.Parameters.Append objCmd.CreateParameter("@FacilityID",      adInteger,      adParamInput,           ,   LOGIN_FACILITY_ID)
    objCmd.Parameters.Append objCmd.CreateParameter("@UserNameExists",      adInteger,      adParamReturnValue)
    objCmd.Execute ,, adExecuteNoRecords
    UserNameExists = objCmd.Parameters("@UserNameExists").Value ' Get value of output parameter



    If UserNameExists>0 Then

        Response.Write ("True")

    Else
        Response.Write ("False")

    End If

End If



%>

JavaScript Ajax Call

<script type="text/javascript">


  function CheckUserName(UserName_element) {


    var UserName = UserName_element.value;
    var valid_UserName_image = document.getElementById('valid_UserName_image');
    var invalid_UserName_image = document.getElementById('invalid_UserName_image');

    $j.ajax({
        data: { action: 'CheckUserName', p_UserName: UserName },
        type: 'GET',
        url: 'page_logic/_check_username_ajax.asp',
        success: function (result) {

            //Just to see what the return value is
            alert(result)

            if (result == "True") {


                valid_UserName_image.style.display = "none";
                invalid_UserName_image.style.display = "inline";

            }
            else {

                valid_UserName_image.style.display = "inline";
                invalid_UserName_image.style.display = "none";

                }

            }
        }
    );

}


</script>

我输入警报(结果)以查看返回的内容,它始终为空。

如果我将存储过程更改为

 SELECT @UserNameExists=0 or SELECT @UserNameExists=1

它将返回一个值,但当然这是对结果进行硬编码而不是实际从数据库获取记录。

1 个答案:

答案 0 :(得分:0)

问题是在SQL Server中的存储过程中使用OUTPUTRETURN之间的混淆。

要清理,您的存储过程需要OUTPUT参数而不是RETURN值,这是不同的。

尝试将输出参数语句更改为;

objCmd.Parameters.Append objCmd.CreateParameter("@UserNameExists", adInteger, adParamOutput)

供参考adParamReturnValue用于返回RETURN语句的值(如果使用),并且仅限于INT值。