本文介绍了如何使用Ajax和ASP.NET WebMethod传递JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用Ajax和ASP.NET WebMethods传递JSON对象时遇到问题
I have a problem with passing a JSON object using Ajax and ASP.NET WebMethods
function setStudentInfo() {
var jsonObjects = [
{ id: 1, name: "mike" },
{ id: 2, name: "kile" },
{ id: 3, name: "brian" },
{ id: 1, name: "tom" }
];
$.ajax({
type: "POST",
url: "ConfigureManager.aspx/SetStudentInfo",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
data: { students: JSON.stringify(jsonObjects) },
success: function (result) {
alert('success');
},
error: function (result) {
alert(result.responseText);
}
});
}
ASP.NET代码
[WebMethod]
public static void SetStudentInfo(object students)
{
//Here I want to iterate the 4 objects and to print their name and id
}
我遇到以下错误:
推荐答案
将整个JSON作为字符串传递,如下所示:
Pass your entire JSON as a string, like this:
data: '{variable: "value"}'
如果我尝试通过它,我总是会出错.
I always get an error if I try to pass it as you have.
这篇关于如何使用Ajax和ASP.NET WebMethod传递JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!