使用Web服务和Ajax jQuery时遇到插入错误

时间:2019-04-10 13:13:04

标签: web-services asp.net-web-api

当我用HTML填写表单时,遇到500错误。我的Web服务无法正常工作。当我复制URL并在新表中打开时,它显示错误以下。

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public void addNewUser(string name,string address,string email, string phoneno, string pin)
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();
            string Message = "";
            try
            {
                using (MySqlConnection connection = new MySqlConnection(DBconnect.ConnectionString))
                {
                    MySqlCommand cmd = new MySqlCommand("INSERT INTO users(name,address,email,phoneno,pin) VALUES(@name,@address,@email,@phoneno,@pin)", connection);
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@address", address);
                    cmd.Parameters.AddWithValue("@email", email);
                    cmd.Parameters.AddWithValue("@phoneno", phoneno);
                    cmd.Parameters.AddWithValue("@pin", pin);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }
                Message = "New User is added Successfully.";
            }
            catch (Exception ex)
            {
                Message = "Not Added";
            }
            var JSonData = new
            {
                Message = Message
            };
            HttpContext.Current.Response.Write(ser.Serialize(JSonData));
        }

<script type="text/javascript">
$(document).ready(function () {
    $("#btnAdduser").on('click', function (e)
    {

    e.preventDefault();
    var All_users = {};  
    All_users.name   = $('#name').val();  
    All_users.address = $('#address').val();  
    All_users.email  = $('#email').val();  
    All_users.phoneno  = $('#phoneno').val();  
    All_users.pin    = $('#pin').val();  
     var jsonData = JSON.stringify({
                    All_users: All_users
                });

        $.ajax({
                    type: "POST",
                    url: "http://localhost/WebApi/WebService.asmx?op=addNewUser",
                    data: jsonData,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    error: OnErrorCall
        });
        function OnSuccess(response) {
                    var result = response.d;
                    if (result == "success") {
                        $("#msg").html("New record addded successfully  :)").css("color", "green");
                    }
                    $("#name").val("");
                    $("#address").val("");
                    $("#email").val("");
                    $("#phoneno").val("");
                    $("#pin").val("");

                }

                function OnErrorCall(response) {
                    $("#msg").html("Error occurs  :(").css("color", "red");
                }
    });
  });

</script>

*

  

System.InvalidOperationException:缺少参数:名称。在   System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection   集合)   System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
  在   System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

*

1 个答案:

答案 0 :(得分:0)

我试图重现您的情况。 问题是UseHttpGet = true,更改为UseHttpGet = false并检查类[System.Web.Script.Services.ScriptService]中的该属性 并像这样通过WebService.asmx/addNewUser更新您的AJAX调用,

<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous"></script>
<button id="btnAdduser">Test</button>
<script type="text/javascript">
$(document).ready(function () {
    $("#btnAdduser").on('click', function (e)
    {

    e.preventDefault();
    var All_users = {};  
    All_users.name   = "Hien";  
    All_users.address = "Test";  
    All_users.email  = "Test";  
    All_users.phoneno  = "Test";  
    All_users.pin    = "123";  
     var jsonData = JSON.stringify({
                    data: All_users
                });

              $.ajax({
                    type: "POST",
                    url: 
                    "http://localhost/WebApi/WebService.asmx/addNewUser",
                    data: JSON.stringify(All_users),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    error: OnErrorCall
        });
        function OnSuccess(response) {
                    var result = response.d;
                    if (result == "success") {
                        $("#msg").html("New record addded successfully  :)").css("color", "green");
                    }
                    $("#name").val("");
                    $("#address").val("");
                    $("#email").val("");
                    $("#phoneno").val("");
                    $("#pin").val("");

                }

                function OnErrorCall(response) {
                    $("#msg").html("Error occurs  :(").css("color", "red");
                }
    });
  });

</script>