jquery - 内部服务器错误 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 天
解决方案
不要使用字符串连接来传递参数,我的朋友。尝试这个:
$.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);
}
});
推荐阅读
- datetime - 如果 Shopify header.liquid 中的语句基于关闭日期时间
- html - 将一些项目与 flex 组合在一起?
- hyperledger-fabric - 使用超级账本结构的 leveldb 会压缩数据吗?
- android - 是否有 API 或 SDK 可以在 SmartPhone for Android TV 上创建远程控制应用程序
- r - 使用 WebGestaltR 加载参考基因列表以进行过度表示分析时发出警告
- python - tf.while_loop -> body 的输入和输出的数量必须匹配 loop_vars: 1, 2
- java - 为什么复制任务中的 gradle exclude 模式不起作用?
- asterisk - 通过python访问RTP统计当前调用的发送包
- html - 检查 html 语法
- selenium - 具有 PageFactory 阻抗的页面对象模型