为什么成功:函数()不起作用

why success: function() doesn't work

本文关键字:不起作用 函数 成功 为什么      更新时间:2023-09-26

我对 J-QUERY 和 JavaScript 相当陌生。 我有几个表单使用一些JavaScript来收集数据并提交到asp页面来处理调用T-SQL过程的请求(编辑用户数据,添加信息,删除等)。 该过程工作正常,但我需要添加一些检查和警报消息,但我没有成功。 这是一个示例

$("#add-user-form").dialog({
    autoOpen: false,
    height: 300,
    width: 420,
    modal: true,
    buttons: {
        'Add User': function () {
            var bValid = true;
            allFields.removeClass('ui-state-error');`enter code here`
            bValid = bValid && checkLength(loginname, "Login Name", 3, 10);
            if (bValid) {
                var statusdataString = '?section=adduser&loginname=' + loginname.val() + '&fullname=' + fullname.val() + '&type=' + type.val();
                $(this).dialog('close');
                $.ajax({
                    type: "GET",
                    url: "admin.asp" + statusdataString
                });
            }
            window.location.reload();
        },
        Cancel: function () {
            $(this).dialog('close');
        }
    },
    close: function () {
        allFields.val('').removeClass('ui-state-error');
    }
});

这是调用的 asp 部分

if (section = "adduser") then
   edituser_sql =  "Exec add_user "  & "'" & request("loginname") & "','" &       
   request("fullname") & "'," & request("type") 
   Conn.execute edituser_sql
   Conn.close
   set Conn = nothing 
   set dsn = nothing 
end if

现在这是我的问题:

  1. 完成所有这些操作后,我需要发出警报,说"用户已添加"或"重复用户"等。 我尝试在成功后添加它:像这样

    $.ajax({类型:"获取",url: "admin.asp"+statusdataString,成功: function(){alert("something")}

    });

但它不起作用。 我还尝试保存过程返回的结果,这告诉我他们是否添加了用户,然后在asp代码中执行此操作

if results= -1 then 
%>
<Script type="text/JavaScript">
alert("something");
</script>
<%

但这也行不通。 我想我理解这个过程,但我不确定所有这些代码是如何工作的,只是零碎的。 您的帮助将不胜感激

成功处理程序不工作的最可能原因是 Web 服务调用不成功。

尝试向 ajax 调用添加一个类似于我的示例的 error: 参数。我正在使用 POST 到 asmx 服务,但您可以将error:部分复制到您的函数中。

    $.ajax({
        type: "POST",
        async: true,
        url: "../webserviceurl",
        data: parameters,
        contentType: "  application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            // Show any returned message alerts
            return;
        },
        error: function (ex) {
            alert("error" + ex.toString());
        }
    });

然后,您可以调试 Web 请求失败的原因。

我会让你的ASP脚本返回一些XML或JSON,描述你想要页面做什么,而不是你的asp页面输出一些javacsript。

编辑:我使ASP代码更加具体,并且我还更改了javascript,以更好地反映如果您使用,例如,KNOCKOUTJS来修改页面,您可以做什么来操作页面。

在你的 ajax 函数中,你可以这样做:

'Add User': function () {
    //....stuff you did...
    $.ajax({
        url: "YourUrl....",
        dataType: "json",
        success: function(response) {
            if (response.status) {
                alert("User added!");
                //do some stuff with response.user
                //if using knockoutjs, just add it to an observable which you would have
                //previously put in the class myViewModel (users is an observable array)
                //this would magically make it appear in your user list
                myViewModel.users.push(new UserModel(response.user));
            }
            else {
                alert("User not created");
            }
        }
        error: function (error) {
            alert(error);
        }
    });
///the rest of your function

此外,您必须摆脱页面重新加载部分。

您的ASP将返回一些格式化如下的JSON,以便与我刚刚编写的内容兼容:

%>
{
    status : <% response.write(response > -1) %>
    user : { <% response.write(aStringInWhichYouHavePutSomeInfoAboutTheUser) %> }
}
%>

另外,我对ASP了解不多,所以我根据我所知道的猜测上面的语法:p