无法从AJAX呼叫向Web服务发送值

时间:2018-10-02 07:38:44

标签: javascript c# jquery ajax

网络服务

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Colaborador GetUserInfo(int idColaborador)
    {
        bool Flag = true;    
        string constr = ConfigurationManager.ConnectionStrings["ControloInterno_ConnectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection(constr))
        {
            string[] Procedure = new string[] { "spOperadores", "spDetalhesColaborador", "spRegDrives" };
            string[] ActionParams = new string[] { "SELECTINF", "SELECTUSR", "SELECTID" };
            Colaborador co = new Colaborador();
            Detalhes dt = new Detalhes();
            Acesso ac = new Acesso();

            for (int i = 0; Flag; i++)
            {
                using (SqlCommand cmd = new SqlCommand(Procedure[i]))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", ActionParams[i]);
                    cmd.Parameters.AddWithValue("@idColaborador", idColaborador);



                    cmd.Connection = con;                       
                    con.Open();                             

                    SqlDataReader rdr = cmd.ExecuteReader();

                    if (Procedure[i] == "spOperadores")
                    {
                        while (rdr.Read())
                        {
                            co.Name = rdr["Nome"].ToString();
                            co.Department = rdr["Departamento"].ToString();
                            co.Profile = rdr["DescricaoPerfil"].ToString();
                            co.Name = rdr["Nome"].ToString();
                            co.Department = rdr["Departamento"].ToString();
                            co.Profile = rdr["DescricaoPerfil"].ToString();
                            co.AdminDate = rdr["dataadmin"].ToString();
                            co.Phone = rdr["tele"].ToString();
                            co.Status = rdr["status"].ToString();
                            co.Observations = rdr["Observacoes"].ToString();
                            co.CreationDate = rdr["DataCriacao"].ToString();
                            co.AltDate = rdr["AltData"].ToString();
                            co.Creator = rdr["criador"].ToString();                                
                        }
                        rdr.Close();

                    }
                    else if (Procedure[i] == "spDetalhesColaborador")
                    {
                        while (rdr.Read())
                        {
                            co.Detalhes = dt;
                            co.Detalhes.Description = rdr["Descricao"].ToString();
                            co.Detalhes.Value = rdr["Value"].ToString();                                
                        }
                        rdr.Close();                            
                    }

                    else if (Procedure[i] == "spRegDrives")
                    {
                        while (rdr.Read())
                        {
                            co.Acesso = ac;
                            co.Acesso.drive = rdr["Drive"].ToString();
                            co.Acesso.tipoAcesso = rdr["TipoAcesso"].ToString();
                            co.Acesso.nivel = rdr["nivel"].ToString();                                
                            Flag = false;
                        }
                        rdr.Close();
                    }
                    cmd.Parameters.Clear();                                // Clear SQLCommand Parameters        
                    con.Close();                                                                                                               
                }                    
            }                  
            return co;
        }
    }

和AJAX调用

 $(document).ready(function () {

        $('#AdminUserNameModal').on('shown.bs.modal', function () {

        var url = "GetColaboradoresWebService.asmx/GetUserInfo";
        var ID = "15";

        $("#UserInfoPanel").html("<div style='text-align:center; border:1px solid red; padding:3px; width:200px'>Please Wait...</div>");

        var request = $.ajax({
            type: "POST",
            url: url,
            data: ID,
            contentType: "application/json; charset-utf-8",
            dataType: "json"
        });

        request.done(function (data) {

            console.log(data);
            var TableContent = "<table class='table table-bordered table-striped'>";


            for (var i = 0; i < data.d.length; i++) {
                TableContent += "<tr>" +
                                        "<td>" + data.d[i].Name + "</td>" +
                                        "<td>" + data.d[i].Detalhes.Description + "</td>" +
                                "</tr>";
                "<tr>" +
                        "<td>" + data.d[i].Name + "</td>" +
                        "<td>" + data.d[i].Detalhes.Description + "</td>" +
                "</tr>";
                "<tr>" +
                        "<td>" + data.d[i].Name + "</td>" +
                        "<td>" + data.d[i].Detalhes.Description + "</td>" +
                "</tr>";

            }
            TableContent += "</table>";
            $("#UserInfoPanel").html(TableContent);

        });
        request.fail(function (data) {
            console.log(data);
        });
    })});

我已经到处搜索,找不到任何可以解决我问题的东西。我也尝试接收字符串,什么也没有。我一直在转换为int或字符串错误,我不明白,我正在尝试发送一个字符串并接收一个字符串,或者将int转换为int仍然给我一个错误?在代码中,我发送一个字符串并接收一个整数,但是我只是在再次更改代码。

2 个答案:

答案 0 :(得分:0)

var idColaborador = 15

请改用此方法,为什么还要将字符串发送到接受int作为参数的方法?

编辑:

您的属性名称必须与方法上的参数名称匹配,以便可以像我上面更改的那样适当地绑定它们,

data:{idColaborador:ID}

答案 1 :(得分:0)

问题解决了,正确的方法是将所有内容都像这样的字符串对待

     data: "{idColaborador: " + idColaborador + " }",