简体   繁体   English

使用Ajax传递json对象的语法

[英]syntax of passing json object using Ajax

Im trying to pass these json object to the code behind 我试图将这些json对象传递给后面的代码

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

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

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

        }

    ]
    };

of course the next step is to stringify the object 当然,下一步是对对象进行字符串化

var data = JSON.stringify(obj);

after that I use a jquery ajax call to pass the value to code behind 之后,我使用一个jQuery ajax调用将值传递给后面的代码

$.ajax({
            url: 'default.aspx/test',
            type: 'POST',
            contentType: 'application/json',
            data: data,
            success: function(result) {
                $('#result').html(result.d);
            }
        });

the point is im getting error form the jquery library 关键是我从jQuery库中获取错误

POST http://localhost:63050/default.aspx/test 500 (Internal Server Error)

the problem is solved when I remove the obj variable, and put it into the JSON.stringfy method, like this 当我删除obj变量并将其放入JSON.stringfy方法时,该问题已解决,就像这样

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

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

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

                }
                ]
            }
        });

Is there anyway to passing the entire object variable into the json.stringify function, instead of declare and initializing within the function? 无论如何,将整个对象变量传递到json.stringify函数中,而不是在函数中进行声明和初始化?

In case you guys wanted to know, my code behind looks something like this 如果你们想知道,我背后的代码看起来像这样

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

No, because in your example you are passing in an object, so you need to put the JSON data into an object for it be parsed as an object. 不,因为在您的示例中,您正在传入一个对象,所以您需要将JSON数据放入对象中,以便将其解析为对象。 Otherwise what you are passing is just an array of variables. 否则,您传递的只是一个变量数组。

Basically, you could correct the problem like this: 基本上,您可以像这样纠正问题:

    $.ajax({
        url: 'default.aspx/test',
        type: 'POST',
        contentType: 'application/json',
        data: { obj: data },
        success: function(result) {
            $('#result').html(result.d);
        }
    });

That way you can use your original stringify. 这样,您可以使用原始的stringify。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM