我创建了一个数据表,如果您单击“加号”按钮,可以在其中添加行。这会将ajax请求发送到以rowId作为参数的url。 (rowId是按下加号按钮的行。)然后,我想获取一个json作为返回值,以便该json用新行更新我的数据表。

这是问题所在:即使我在网络监视器中看到了我的Ajax请求,也从未到达成功部分。有人知道为什么吗?

<table class="table display" id="tabelle_benutzerHead" cellspacing="0" width="100%">
    <thead >
        <tr>
            <th>Uhrzeit</th>
            <th>SpNr</th>
            <th>Platz</th>
            <th>Heimmannschaft</th>
            <th>Gastmannschaft</th>
            <th>Freispiele</th>
        </tr>
    </thead>
    <tbody>
            <tr id="Row1">
                <td>08:00</td>
                <td>134</td>
                <td>Platz 1</td>
                <td>Team 5</td>
                <td>Team 3</td>
                <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td>
            </tr>
            <tr id="Row2">
                <td>09:00</td>
                <td>76</td>
                <td>Platz 3</td>
                <td>Team 7</td>
                <td>Team 1</td>
                <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td>
            </tr>
            <tr id="Row3">
                <td>17:30</td>
                <td>45</td>
                <td>Platz 11</td>
                <td>Team 2</td>
                <td>Team 9</td>
                <td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td>
            </tr>
    </tbody>
</table>


var oTable = $('#tabelle_benutzerHead').DataTable({
    responsive: true,
    "bAutoWidth": false,
    "bFilter": false,
    "info": false,
    "scrollCollapse": true,
    "paging": false,
    rowReorder: true
})

oTable.on('row-reorder', function(e, diff, edit) {

    var draggedRow = diff[(diff.length - 1)].node;
    var draggedRowId = $(draggedRow).attr('id');    <!-- dragged and dropped Row -->
    var previousRow = diff[(diff.length - 2)].node;
    var previousRowId = $(previousRow).attr('id');  <!-- the row before the dragged and dropped -->

    $.ajax({
        type: "POST",
        url: "myurl.html",
        data: {
            draggedRowId,
            previousRowId
            }
    });
});

var uhrzeit;
var spNr;
var platz;
var heimmannschaft;
var gastmannschaft;

$(function() {

    $('#tabelle_benutzerHead').delegate('button.AddDaten', 'click', function() {
        var row = $(this).closest('tr'); // get the parent row of the clicked button

        var tableRowAppend = '<tr><td>' + uhrzeit + '</td><td>' + spNr + '</td><td>' + platz + '</td><td>'+ heimmannschaft + '</td><td>'+ gastmannschaft +
                            '</td><td><button class="btn btn-default AddDaten" type="button"><i class="glyphicon glyphicon-plus-sign"></i></button></td></tr>';

        var rowId = $(row).attr('id'); // clicked RowId

        $.ajax({
            type: "POST",
            url: "myurl.html",
            data: {
                rowId
            },
            dataType: 'json',
            contentType: "application/json",
            success: function(data) {
                var dataJson =  [
                    "10:30",
                    "77",
                    "Platz 1",
                    "Team 7",
                    "Team 12",
                    "<button class='btn btn-default AddDaten' type='button'><i class='glyphicon glyphicon-plus-sign'></i></button>"
                ];

                $.getJSON(dataJson, function(){
                    oTable.clear();
                    oTable.rows.add(dataJson);
                    oTable.draw();
                });
            }
        });
    });
});

最佳答案

.success在这里充当回调函数。您是在发回一些东西作为回应吗?
即当发布到myurl/operationOnData

operationOnData(req,res){ // do something here with req, than res.send("Got it!");}

关于javascript - Ajax请求成功使用json作为返回值不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41075074/

10-13 08:56