为什么Ajax不打webmethod功能?

时间:2018-08-19 10:38:33

标签: javascript jquery asp.net ajax asp.net-ajax

我正在Asp.net Web表单中使用Jquery Ajax。但是Ajax调用不会影响我的Web方法功能。我多次尝试了堆栈溢出的解决方案。但是它仍然无法正常工作。我还在webmethod函数中添加了断点。但是ajax没有命中它。它只显示了错误警报。我已经尝试了五个多小时,但仍无法正常工作。这是我的代码:

jquery Ajax代码:

CardView

LabEntryUI.aspx.cs代码:

RecyclerView

LabEntry类代码:

 <script>
    $(document).ready(function () {
        document.getElementById("entryDate").value = moment().format('DD/MM/YYYY');
        var eTime = moment().format('HH:00');
        document.getElementById("entryTime").value = eTime;

        $("#saveButtonLabEntry").click(function () {
            alert("hi");

            var today = moment().format('YYYY/MM/DD');

            var eTime = moment().format('HH:00');

            var unitName = $('#unitName').val();

            var labEntryArray = [];
            var tbl = document.getElementById("myTable1");
            var rCount = tbl.rows.length;


            alert(rCount);
            try {
                for (var j = 1; j <= rCount - 1; j++) {
                    var sampleName = tbl.rows[j].cells[1].innerText;
                    var density15C = tbl.rows[j].cells[2].children[0].value;
                    var rvpPsi = tbl.rows[j].cells[3].children[0].value;
                    var colourAstm = tbl.rows[j].cells[4].children[0].value;
                    var fp0C = tbl.rows[j].cells[5].children[0].value;
                    var viscosity50C = tbl.rows[j].cells[6].children[0].value;
                    var viscosity100C = tbl.rows[j].cells[7].children[0].value;
                    var pp0C = tbl.rows[j].cells[8].children[0].value;
                    var ibp = tbl.rows[j].cells[9].children[0].value;
                    var astm5 = tbl.rows[j].cells[10].children[0].value;
                    var astm10 = tbl.rows[j].cells[11].children[0].value;
                    var astm50 = tbl.rows[j].cells[12].children[0].value;
                    var astm90 = tbl.rows[j].cells[13].children[0].value;
                    var astm95 = tbl.rows[j].cells[14].children[0].value;
                    var fbp = tbl.rows[j].cells[15].children[0].value;
                    var beforeOxid = tbl.rows[j].cells[16].children[0].value;
                    var afterOxid = tbl.rows[j].cells[17].children[0].value;
                    var report = tbl.rows[j].cells[18].children[0].value;
                    var fr5 = tbl.rows[j].cells[19].children[0].value;

                    //alert(fr5);
                    var obj = { unitname: unitName, SampleName: sampleName, Entrydate: today, EntryTime: eTime, Density15C: density15C, RVP_PSI: rvpPsi, colourASTM: colourAstm, FP: fp0C, Viscosity50: viscosity50C, Viscosity100: viscosity100C, pp: pp0C, ASTM_IBP: ibp, ASTM5: astm5, ASTM10: astm10, ASTM50: astm50, ASTM90: astm90, ASTM95: astm95, FBP: fbp, BSWB4Oxid: beforeOxid, BSWAfterOxid: afterOxid, Report: report, FR5xylene: fr5 };

                    labEntryArray.push(obj);
                }

            } catch (e) {
                alert("hello");
            }
            var pageUrl = '<%=ResolveUrl("LabEntryUI.aspx")%>';



            $.ajax(
            {
                //url: "LabEntryGateway.aspx/SaveLabData",
                url: pageUrl + '/SaveLabData',
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(labEntryArray),
                success: function (da) {
                    //$("#msg").html(da.Message);
                    alert('success');
                },
                error: function (da) {
                    alert('hello Error');
                }

            });


        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

尝试更新您的jquery代码以发布这样的数据。这里的json表示一个具有一个属性labEntryArray的对象,其中包含一个LabEntry对象数组

data: JSON.stringify({labEntryArray:labEntryArray})

代替

data: JSON.stringify(labEntryArray)