6

我在使用 Ajax 和 ASP.NET WebMethods 传递 JSON 对象时遇到问题

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
}

我收到以下错误:

"{"Message":"无效的 JSON 原语:学生。","StackTrace":" 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度)在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer 序列化程序)在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)在系统。 Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序)在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context) 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.参数异常"}"

4

5 回答 5

6

将整个 JSON 作为字符串传递,如下所示:

data: '{variable: "value"}'

如果我像您一样尝试通过它,我总是会收到错误消息。

于 2012-11-19T07:16:35.877 回答
5

我知道这是一个老问题,但如果有人来这里寻求答案,这就是解决方案;

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: JSON.stringify({ students: jsonObjects }),
    success: function (result) {
        alert('success');
    },
    error: function (result) {
        alert(result.responseText);
    }

});
于 2015-07-10T17:31:32.057 回答
3

您收到该错误是因为需要一个对象,但您的代码需要一个对象列表。有时这可能有点棘手。为了使数据传输更容易,您应该创建一个具有要传递给 WebMethod 的对象类型的属性的类,因为 ASP.NET 似乎更容易解析它。例如:

public class Student
{
    private string _name;
    private int _id;
    public string name {
        get { return _name; }
        set { _name = value; }
    }
    public int id {
        get { return _id; }
        set { _id = value; }
    }
}

然后您的 WebMethod 将更改为接受学生类对象列表,如下所示:

[WebMethod]
public static void SetStudentInfo(List<Student> Students)
{
    foreach (Student s in Students)
    {
        //Do whatever here System.Console.WriteLine(s.name);
    }
}

然后你需要做的就是稍微改变你的 Ajax 调用,如下所示:

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);
        }

    });
}
于 2012-11-19T11:48:58.300 回答
1

试试这个代码:

function setStudentInfo() {

var jsonObjects = {"students" : [
    { 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: JSON.stringify(jsonObjects),
    success: function (result) {
        alert('success');
    },
    error: function (result) {
        alert(result.responseText);
    }

});

}

于 2013-10-07T09:29:24.820 回答
0

您实际上并没有将 json 发送到服务器,发送 json 只需为 data 属性传递一个 json 字符串。

data: JSON.stringify(jsonObjects),
于 2012-11-19T07:16:17.717 回答