如何获得正确的$ .ajax数据类型

时间:2013-03-08 18:32:59

标签: javascript jquery ajax

你可以帮忙解决这个问题吗?我有以下javascript:

$('form').click(function (e)
           {
            if (e.target.getAttribute('id') === 'SubmitAddLevel')
            {
                var parent = $('#' + e.target.getAttribute('attr')),
                var Data = [];
                parent.find('.input').children().each(function (i, e)
                {
                  Data.push(e.getAttribute('id') + ":" + e.value);
                    console.log(Data);
                });                

                $.ajax({
                    type: "POST",
                    url: 'AjaxControls.aspx/CreateUserLevel',
                    //data: Data, //.join(','),
                    dataType: "text",
                    contentType: "application/json; charset=utf-8",
                    //error: function (er) { alert(er); },
                    success: function (response)
                    {

                        if (response.d === "true")
                        {

                            $("#ErrorDivAddLevel").html('Level created successfully!').fadeIn('slow');
                        }
                        else
                        {
                            $("#SuccessDivAddLevel").html('Level creation failed!').fadeIn('slow');
                        }
                    },

                });
            }

我在控制台上获得的“数据”的结果是:[“LevelNameAddLevel:Admin”,“PriviledgeIDAddLevels:| 1 | 2 | 3 | 4 | 5 | 6 | 7 |”]。如何将此转换为ajax将传递给我的web menthod?

这是网络方法

<WebMethod(EnableSession:=True)>
 Public Shared Function CreateUserLevel(userLevel As String, userPriviledges As String) As String

 return "true"

end function

1 个答案:

答案 0 :(得分:0)

我认为您的数据看起来应该更像这样:

[{"LevelNameAddLevel":"Admin"}, {"PriviledgeIDAddLevels":"|1|2|3|4|5|6|7|"}]

所以你在数组中有键/值对。在请求中,您应该能够通过请求中的密钥获取数据。 但我不太确定这是什么意思:“| 1 | 2 | 3 | 4 | 5 | 6 | 7 |”