Wednesday, 7 October 2020

Ajax post calls several times

I have the following code:

                <div id="assignWindow" style="display:none; width:500px">
                    Invoice # <input id="invoiceNumber" name="invoiceNumber" /><br />
                    Amount: <input id="amount" name="amount" /><br />
                    <select id="carrierList" name="carrierList" style="width:400px;"></select>
                    <br />
                    <button type="button" id="acceptCarrierButton" name="acceptCarrierButton" class="btn btn-primary">@T("Common.Send")</button>
                </div>



                        $('#acceptCarrierButton').on('click', function (e) {
                            var valid = true;
                            var amount = null;
                            if ($('#amount').val() != '') {
                                if (validatePrice($('#amount').val()))
                                    amount = $('#amount').val();
                                else {
                                    alert('invalid amount');
                                    valid = false;
                                }
                            }
                            if (valid) {
                                $.ajax({
                                    type: "POST",
                                    dataType: "json",
                                    url: '@Url.Action("AssignDevice", "Tenant")',
                                    data: {
                                        carrierId: $('#carrierList').val(),
                                        deviceId: dataItem.Id,
                                        invoiceNumber: $('#invoiceNumber').val(),
                                        amount: amount
                                    },
                                    success: function (data) {
                                        $('#invoiceNumber').val('');
                                        $('#amount').val('');
                                        assignWindow.close();
                                        $("#devices-grid").data("kendoGrid").dataSource.read();
                                    },
                                    error: function (data) {
                                        alert(data.responseText);
                                    }
                                });
                            }
                        });

first call calls backend method only once, second call calls 2 times, third call calls 3 times etc... why so and how to correct it?



from Ajax post calls several times

No comments:

Post a Comment