使用AJAX将JSON对象发送到WebMethod

时间:2016-07-08 14:57:10

标签: jquery asp.net json ajax webmethod

我从HTML表单中提取数据并将其转换为JSON对象以发送到WebMethod。

现在它说我得到了成功的回复,但我不确定如何检查它是否正确发送到WebMethod。我已尝试在VS2015中进行调试,但似乎无法达到断点。

如何测试我是否正确设置了这一切?

这是我的HTML:

 <form id="addForm" name="addForm">
     <input type="text"  name="playername" id="playername" placeholder="Player"/> 
     <input type="text" name="points" id="points" placeholder="Points" />
     <input type="text" name="steals" id="steals" placeholder="Steals" />
     <input type="text" name="blocks" id="blocks" placeholder="Blocks" /> 
     <input type="text" name="assists" id="assists" placeholder="Assists" />
     <input type="text" name="mpg" id="mpg" placeholder="MPG" /> 
     <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" />
     <input type="text" name="threepct" id="3pct" placeholder="3 %" /> 
     <input type="button" value="add player" id="addbtn" name="addbtn" />
     </form>

我的Ajax

$("#addbtn").click(function () {
                var form = JSON.stringify($("form").serializeArray());


                $.ajax({
                    method: 'POST',
                    url: "players.aspx/addRow",
                    data: form,
                    success: function (data) {
                        alert('success');
                    },
                    error: function () {
                        alert('failure');
                    }
                });
              });

以及我尝试发送数据的网络方法

[WebMethod]
        public static void addRow(string form)
        {

            dynamic players = JObject.Parse(form);
        }

我想知道播放器中有哪些数据(如果有的话)(可能是AJAX的误报)。

2 个答案:

答案 0 :(得分:0)

替换

data: form,

data: "{form:" + form + "}",

并使用VS2015将调试器放入WebMethod内,以查看您为addRow方法字符串变量form获取的值。

答案 1 :(得分:0)

代码背后:

public partial class players : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [System.Web.Services.WebMethod]
    public static void addRow(Player player)
    {
        System.Diagnostics.Debugger.Break();
        //dynamic players = JObject.Parse(form);
    }
}

public class Player
{
    public string playername { get; set; }
    public string points { get; set; }
    public string steals { get; set; }
    public string blocks { get; set; }
    public string assists { get; set; }
    public string mpg { get; set; }
    public string shotpct { get; set; }
    public string _3pct { get; set; }
}

<强> .ASPX:

<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {

            $("#addbtn").click(function () {
                var player =
                    {
                        "playername": $("#playername").val(),
                        "points": $("#points").val(),
                        "steals": $("#steals").val(),
                        "blocks": $("#blocks").val(),
                        "assists": $("#assists").val(),
                        "mpg": $("#mpg").val(),
                        "shotpct": $("#shotpct").val(),
                        "_3pct": $("#3pct").val(),
                    };

                $.ajax({
                    type: "POST",
                    url: "players.aspx/addRow",
                    contentType: "application/json",
                    data: JSON.stringify({ player: player }),
                    success: function (data) {
                        alert('success');
                    },
                    error: function (errordata) {
                        alert('failure');
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="addForm" name="addForm">
        <input type="text" name="playername" id="playername" placeholder="Player" />
        <input type="text" name="points" id="points" placeholder="Points" />
        <input type="text" name="steals" id="steals" placeholder="Steals" />
        <input type="text" name="blocks" id="blocks" placeholder="Blocks" />
        <input type="text" name="assists" id="assists" placeholder="Assists" />
        <input type="text" name="mpg" id="mpg" placeholder="MPG" />
        <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" />
        <input type="text" name="threepct" id="3pct" placeholder="3 %" />
        <input type="button" value="add player" id="addbtn" name="addbtn" />
    </form>
</body>