0

我有一个带有 c#(.net4) 代码的表单。在这个表格中,用户填写他的规范并提交。
我想在 jquery 中使用 ajax 或 post 方法来防止闪烁。我编写流动代码。“成功”函数执行但它不起作用并且任何记录插入数据库中;我单独执行executemember方法。它没有问题但它不适用于jquery ajax。哪里有问题?

[WebMethod]
    public static string executeinsert(string name ,string family , string username,string password , string  email,string tel, string codemeli)
    {   string constring = "data source=.;database=site;integrated security=true;";

        SqlConnection con = new SqlConnection(constring);
        SqlCommand com = new SqlCommand("insertmember", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 250));
        com.Parameters["@username"].Value = username;
        com.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 150));
        com.Parameters["@name"].Value = name;
        com.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
        com.Parameters["@password"].Value = password;
        com.Parameters.Add(new SqlParameter("@family", SqlDbType.NVarChar, 250));
        com.Parameters["@family"].Value = family;
        com.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50));
        com.Parameters["@email"].Value = email;
        com.Parameters.Add(new SqlParameter("@codemeli", SqlDbType.NChar, 10));
        com.Parameters["@codemeli"].Value = codemeli;
        com.Parameters.Add(new SqlParameter("@tel", SqlDbType.NChar, 12));
        com.Parameters["@tel"].Value = tel;
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        return "success";
        }

及其我的jQuery代码

 <script type="text/javascript">
    $(document).ready(
    function () {
        $("#Button1").click(
            function () {
                var username, family, name, email, tel, codemeli, password;
                username = $('#<%=TextBox1.ClientID%>').val();
                name = $('#<%=TextBox2.ClientID%>').val();
                family = $('#<%=TextBox3.ClientID%>').val();
                password = $('#<%=TextBox4.ClientID%>').val();
                email = $('#<%=TextBox5.ClientID%>').val();
                tel = $('#<%=TextBox6.ClientID%>').val();
                codemeli = $('#<%=TextBox7.ClientID%>').val();

                $.ajax(
                {
                    type: "POST",
                    url: "WebApplication20.aspx/executeinsert",
                    data: "{'username':'username','name':name,
                            'family':family,'password':password,
                            'email':email,'tel':tel,
                            'codemeli':codemeli}",
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    async: true,
                    cache: false,
                    success: function(msg) {
                        alert(msg);
                    },
                    error: function (x, e) {
                        alert("The call to the server side failed. " 
                              + x.responseText);
                    }
                }
            );
        }
     )
 })
</script>

谢谢

4

3 回答 3

1

我相信您的 JSON 格式不正确

尝试在此处验证您的 json 。

您可以看到您的 json 无效。

于 2012-07-06T06:26:33.823 回答
1

看一下这个:

$(document).ready(function () {
    $("#Button1").click(function () {
        var userData = new Object();

        userData.username = $('#<%=TextBox1.ClientID%>').val();
        userData.name = $('#<%=TextBox2.ClientID%>').val();
        userData.family = $('#<%=TextBox3.ClientID%>').val();
        userData.password = $('#<%=TextBox4.ClientID%>').val();
        userData.email = $('#<%=TextBox5.ClientID%>').val();
        userData.tel = $('#<%=TextBox6.ClientID%>').val();
        userData.codemeli = $('#<%=TextBox7.ClientID%>').val();

        $.ajax({
            type: "POST",
            url: "WebApplication20.aspx/executeinsert",
            data: userData,
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (msg) {
                alert(msg);
            },
            error: function (x, e) {
                alert("The call to the server side failed. " + x.responseText);
            }
        });
    });
});

PS如果它调用的话,尝试调试并在webmethod中放置断点。

于 2012-07-06T06:28:25.660 回答
0

jsonis not in right 格式 'name':name不包含在,'string

data: "{'username' : 'username',   'name':name,'family':family,'password':password,'email':email,'tel':tel,'codemeli':codemeli    }",

它应该是

   data: "{'username' : 'username', 'name':'name', 'family':'family', 'password':'password', 'email':'email', 'tel':'tel', 'codemeli':'codemeli'}",
于 2012-07-06T06:14:34.283 回答