ajax调用首先失败两次,然后从第三次返回成功

时间:2018-07-25 05:41:14

标签: c# jquery ajax webmethod

<button type="button" id="btnSubmit" class="btn btn-success" runat="server" onserverclick=`enter code here`"Submitbtn_ServerClick">Submit</button> 

<script>       
    $(document).ready(function () {
        $("#<%=btnSubmit.ClientID%> ").click(function () {

            var Tabledata = new Array();

            $('#<%=AbsenceInfoTable.ClientID%> tr').each(function (row, tr) {

                console.log("entered each");
                Tabledata[row] = {
                    "DateOfAbsence": $(tr).find('td:eq(0)').text(),
                    "Reason": $(tr).find('td:eq(1)').text(),
                    "AdminAbsenceReason": $(tr).find('td:eq(2)').text(),
                    "DayType": $(tr).find('td:eq(3)').text()

                }
                console.log(Tabledata[row]);

            });

            Tabledata.shift(); //first row is the table header - so remove
            console.log("Just the Table Data" + Tabledata);

            $.ajax({
                type: 'POST',
                url: 'table.aspx/GetAbsenceData',
                contentType: "application/json",
                dataType: "json",
                data:  "{'items':" + JSON.stringify(Tabledata) + "}",
                success: function getSuccess() { alert("Success: "); },
                error: function getFail(jqXHR, textStatus, errorThrown) { alert("Fail: " + jqXHR.status + "error : " + errorThrown.text) }

            });

        });

    });

</script>

[System.Web.Services.WebMethod]
    public static void GetAbsenceData(List<object> items)
    {}

在所有情况下,均会正确生成Json对象。单击“提交”按钮时将进行回发,以清除先前的输入。

对于单击提交按钮的前2次,警报返回失败:0错误:未定义。

然后从警报框第三次显示成功。

我不了解此行为的原因。 谢谢。

0 个答案:

没有答案