首页 > 解决方案 > 内部服务器错误 ajax asp.net

问题描述

嗨,我正在编写一个 ajax 代码,该代码使用 Web 服务从数据库服务器获取数据,但每次我尝试获取数据时都会出错:内部服务器错误

我在这里做错了什么..

$.ajax({
       type: "POST",
                url: "CreerEquipe.aspx/GetFrmtionShema",
                data: "{'id':'" + parseInt(id) + "','formation':'4-4-2'}",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (res) {
                    alert(res.d);

                    $('.formation').html(res.d);
                    //alert(schema);

                    //$("#TotalDEF").html("(" + Count + "/5)");
                },
                error: function (xhr, status, error) {
                    alert('Error : ' + error);
                }
            });

webmethode 中的另一端:

        [WebMethod]
        [ScriptMethod]
        public static string GetFrmtionShema(int id,string formation)
        {
            string data = "";
            ConGeters Config = new ConGeters();
            Config.Cmd = new SqlCommand("select * from tbl_Formation where id_formation = @id", Config.con);

            Config.Cmd.Parameters.AddWithValue("@id", id);
            //Config.Cmd.CommandType = CommandType.StoredProcedure;
            Config.Open();
            Config.Dr = Config.Cmd.ExecuteReader();
            while (Config.Dr.Read())
            {
                data = Config.Dr[4].ToString();
            }
            Config.Close();
            Config.Dr.Close();
            return data;
        }

请问有什么建议吗?我被封锁了 2 天

标签: jqueryasp.netajax

解决方案


不要使用字符串连接来传递参数,我的朋友。尝试这个:

$.ajax({
       type: "POST",
                url: "CreerEquipe.aspx/GetFrmtionShema",
                data: JSON.stringify({ id: parseInt(id), formation: '4-4-2' }),,
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (res) {
                    alert(res.d);

                    $('.formation').html(res.d);
                    //alert(schema);

                    //$("#TotalDEF").html("(" + Count + "/5)");
                },
                error: function (xhr, status, error) {
                    alert('Error : ' + error);
                }
            });

推荐阅读