C# 使用Ajax传递json对象的语法

C# 使用Ajax传递json对象的语法,c#,jquery,asp.net,json,C#,Jquery,Asp.net,Json,我试图将这些json对象传递给代码隐藏 var obj = { Name: '1', Starting: '3', Timeline: [ { StartTime: '111', GoesFor: '111' } , { StartTime: '112', GoesFor: '112'

我试图将这些json对象传递给代码隐藏

var obj = {
        Name: '1',
        Starting: '3',

        Timeline: [
        {
            StartTime: '111',
            GoesFor: '111'

        }
        ,
        {
            StartTime: '112',
            GoesFor: '112'

        }

    ]
    };
$.ajax({
            url: 'default.aspx/test',
            type: 'POST',
            contentType: 'application/json',
            data: data,
            success: function(result) {
                $('#result').html(result.d);
            }
        });
当然,下一步是字符串化对象

var data = JSON.stringify(obj);
之后,我使用jqueryajax调用将值传递给代码隐藏

var obj = {
        Name: '1',
        Starting: '3',

        Timeline: [
        {
            StartTime: '111',
            GoesFor: '111'

        }
        ,
        {
            StartTime: '112',
            GoesFor: '112'

        }

    ]
    };
$.ajax({
            url: 'default.aspx/test',
            type: 'POST',
            contentType: 'application/json',
            data: data,
            success: function(result) {
                $('#result').html(result.d);
            }
        });
关键是我从jquery库中得到了错误

POST http://localhost:63050/default.aspx/test 500 (Internal Server Error)
当我删除obj变量并将其放入JSON.stringfy方法时,问题就解决了,如下所示

var data = JSON.stringify({
            obj: {
                Name: '1',
                Starting: '3',

                Timeline: [
                {
                    StartTime: '111',
                    GoesFor: '111'

                }
                ,
                {
                    StartTime: '112',
                    GoesFor: '112'

                }
                ]
            }
        });
public class MyModel
{
    public string Name { get; set; }
    public string Starting { get; set; }
    public testing2[] Timeline { get; set; }
}

public class testing2
{
    public string StartTime { get; set; }
    public string GoesFor { get; set; }
}

[WebMethod]
    public static string Test(MyModel obj)
    {
        return "Hello from test" + obj.Name + obj.Starting + obj.Timeline[0].StartTime + obj.Timeline[1].StartTime;
    }
是否需要将整个对象变量传递到json.stringify函数中,而不是在函数中声明和初始化

如果你们想知道的话,我的代码是这样的

var data = JSON.stringify({
            obj: {
                Name: '1',
                Starting: '3',

                Timeline: [
                {
                    StartTime: '111',
                    GoesFor: '111'

                }
                ,
                {
                    StartTime: '112',
                    GoesFor: '112'

                }
                ]
            }
        });
public class MyModel
{
    public string Name { get; set; }
    public string Starting { get; set; }
    public testing2[] Timeline { get; set; }
}

public class testing2
{
    public string StartTime { get; set; }
    public string GoesFor { get; set; }
}

[WebMethod]
    public static string Test(MyModel obj)
    {
        return "Hello from test" + obj.Name + obj.Starting + obj.Timeline[0].StartTime + obj.Timeline[1].StartTime;
    }

不,因为在您的示例中,您传递的是一个对象,所以您需要将JSON数据放入一个对象中,以便将其解析为一个对象。否则,您传递的只是一个变量数组

基本上,您可以这样纠正问题:

    $.ajax({
        url: 'default.aspx/test',
        type: 'POST',
        contentType: 'application/json',
        data: { obj: data },
        success: function(result) {
            $('#result').html(result.d);
        }
    });
这样您就可以使用原始的stringify