我在使用Ajax将动态数据发布到ASP.NET Code Behind函数时遇到错误500内部错误。我不明白我哪里错了。请帮助。
我的Ajax代码:
function SaveData() {
// Check What's Getting Updated
for(var i in changedData){
console.log(changedData[i]);
}
// Ajax Call
$.ajax({
type: "POST",
url: "StudentHandsOn.aspx/SaveData",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(changedData),
success: function (changes) {
console.log('Data Updated: '+changedData);
},
error: function (thrownError) {
alert("Error" + thrownError);
}
});
}
我的C#CodeBehind是:
[WebMethod]
public static void SaveData(List<Student> students)
{
StudentHandsOn handsOn = new StudentHandsOn();
handsOn.displayMessage();
}
public void displayMessage()
{
Response.Write("<script>alert('Function Called.... Working Fine ');</script>");
}
我已经使用SaveData中的for循环检查了ChangedData数组中的内容,并且运行正常。
["st_01", "Albert ", 3, "CE ", "Angela ", 70, 90, 90, 250, 83.33333333333333, "B "] StudentHandsOn.aspx:218
["st_02", "Inger ", 2, "CE ", "Angela ", 67, 98, 99, 264, 88, "B "] StudentHandsOn.aspx:218
POST http://localhost:53600/StudentHandsOn.aspx/SaveData 500(内部服务器错误)
更新
我在代码中添加了几行代码:
var studentData = JSON.stringify(changedData) ;
console.log(studentData);
并将SaveData方法中的Parameter类型更改为List。我在控制台中输出为。
[["st_01","Albert ",3,"CE ","Angela ",70,90,90,250,83.33333333333333,"B "],["st_02","Inger ",2,"CE ","Angela ",67,98,99,264,88,"B "],["st_03","Hamlet ",1,"EE ","Emily ",60,60,60,180,60,"D "],["st_04","Jiny ",4,"CS ","Vannila ",100,80,70,250,83.33333333333333,"B "]] StudentHandsOn.aspx:218
POST http://localhost:53600/StudentHandsOn.aspx/SaveData 500(内部服务器错误)
我现在正在获得一个数组数组。如何将此代码传递给方法?
答案 0 :(得分:-1)
尝试将数据代码更改序列化为:
data: '{"students": "' + JSON.stringify(changedData) + '"}',