如何使用Ajax和ASP.NET WebMethod传递JSON对象 [英] How to pass JSON object using Ajax with ASP.NET WebMethod

查看:221
本文介绍了如何使用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
}

我遇到以下错误:

"{" 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) 在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入) 在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化器) 在System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext上下文) 在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)," ExceptionType:" System.ArgumentException}"

"{"Message":"Invalid JSON primitive: students.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input) at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer) at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}"

推荐答案

将整个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对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆