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

时间:2012-06-27 14:53:59

标签: javascript jquery asp.net ajax

我是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({ 类型:“GET”, url:“admin.asp”+ statusdataString, 成功:function(){alert(“something”)}

    });

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

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

    但这也不起作用。我想我理解这个过程,但我不确定所有这些代码是如何工作的,只是点点滴滴。非常感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您的成功处理程序无法正常工作的最可能原因是因为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请求失败的原因。

答案 1 :(得分:0)

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

编辑:我已经使ASP代码更加具体了,我还改变了javascript,以便更好地反映出如果你使用敲门来修改页面,你可能会做些什么来操纵页面。

在你的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

相关问题