通过jQuery使用Web服务保存数据

时间:2014-05-25 10:41:00

标签: c# javascript jquery web-services

编辑:请参阅我的网站:www.fredm.co.za,然后点击注册,您将看到返回的错误消息。

我正在尝试使用jQuery访问将用户添加到数据库的Web服务。 这就是我的尝试:

jQuery的:

    $(document).ready(function()
    {
        $('#btnSignUp').click(function()
        {
            try
            {
                $.ajax({
                    type: "POST",
                    url: "Some_Web_Service.asmx",
                    data: '{EmailAddress: "fred", FirstName: "fred", Surname: "mol", Birthday: "1991/11/25", Password: "1346", Question: "What?", Answer: "Yes"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: OnFailure
                });
            }
            catch(ex)
            {
                OnFailure(ex.message)
            }
        });
    });

    function OnSuccess(response)
    {
        alert(response.d);
    }

    function OnFailure(response)
    {
        alert(response.d);
    }

这是我的Web服务(Some_Web_Servive.asmx):

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public bool JQuerySignUp(string EmailAddress, string FirstName, string Surname, string Birthday, string Password, string Question, string Answer)
        {
            bool success = false;
            try
            {
                _stringBuilder = new StringBuilder();
                _stringBuilder.AppendLine("INSERT INTO tblJQueryFredMUsers");
                _stringBuilder.AppendLine("(");
                _stringBuilder.AppendLine("    EmailAddress,");
                _stringBuilder.AppendLine("    FirstName,");
                _stringBuilder.AppendLine("    Surname,");
                _stringBuilder.AppendLine("    Birthday,");
                _stringBuilder.AppendLine("    Passwrd,");
                _stringBuilder.AppendLine("    Question,");
                _stringBuilder.AppendLine("    Answer");
                _stringBuilder.AppendLine(")");
                _stringBuilder.AppendLine("VALUES");
                _stringBuilder.AppendLine("(");
                _stringBuilder.AppendLine("    @EmailAddress,");
                _stringBuilder.AppendLine("    @FirstName,");
                _stringBuilder.AppendLine("    @Surname,");
                _stringBuilder.AppendLine("    @Birthday,");
                _stringBuilder.AppendLine("    @Passwrd,");
                _stringBuilder.AppendLine("    @Question,");
                _stringBuilder.AppendLine("    @Answer");
                _stringBuilder.AppendLine(")");

                _params = new List<SqlParameter>();
                _params.Add(new SqlParameter("@EmailAddress", EmailAddress));
                _params.Add(new SqlParameter("@FirstName", FirstName));
                _params.Add(new SqlParameter("@Surname", Surname));
                _params.Add(new SqlParameter("@Birthday", Birthday));
                _params.Add(new SqlParameter("@Passwrd", Password));
                _params.Add(new SqlParameter("@Question", Question));
                _params.Add(new SqlParameter("@Answer", Answer));

                throw new ArgumentException("asdadada");

                ExecuteQuery(_stringBuilder.ToString(), _params);
                success = true;
            }
            catch(Exception ex)
            {
                SendEmailErrorMessage(ex.Message);
                //TODO: Log the details somewhere so that we can look at it later.
                //throw;
            }
            finally
            {
                CloseDatabase();
            }
            return success;
        }

因此,当我将其发布在我网站的根文件夹上,然后上网并访问我的网站空间时,我看到了这一点: enter image description here

如果我点击那里,会打开: enter image description here

当我填写此内容时,将保存到数据库中 但在我的网站上,当我点击按钮时,没有任何反应。 我做错了吗?

0 个答案:

没有答案