This is a migrated thread and some comments may be shown as answers.

OnSave preventDefault in AJAX call

3 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Scott
Top achievements
Rank 1
Scott asked on 23 Mar 2011, 11:35 PM
Greetings,

I'm having some strange behavior with the the OnSave e.preventDefault() event firing correctly when inside an ajax call in my Grid editor.  Here's the code snippet for the call:

function onSave(e) {
           var masterRow = $(e.form).closest('.t-detail-row').prev();
            var parentDataItem = masterRow.closest('.t-grid').data('tGrid').dataItem(masterRow);
            var mode = e.mode;
            var grid = $(this).data('tGrid');
            var values = e.values;
            //Grab the parent record visinumber to be used in the ajax call below
            var parentVisitNumber = parentDataItem['VisitNumber'];
            var chData = true;
            
           $.ajax({
                type: "POST",
                url: "<%= ResolveUrl("~/TradingPost/GetSingleCamperRowRecord") %>",
                data: "visitNumber=" + parentVisitNumber,
                success: function (result) {
                    var depositItem = result.CamperTPDeposit;
                    var spentItem = result.CamperTPTotal;
                    var balanceItem = result.CamperTPRemainingBalance;
                    if (spentItem == null){
                        spentItem = "0"
                    }


                    if(values.Amount > balanceItem)
                    {
                    chData = false;
                    alert("The amount you are trying to submit is greater than the available balance.");
                    e.preventDefault();
                    
                    }
                    
                },
                error: function (req, status, error) {


                    alert("Error occured.");


                }
                 
            });
....

Two odd things are happening:  1.)  The value of chData does not change.  If I check it's value outside the ajax() call, it is still true, even though it has been set to false in the ajax code.  2.)  If I have any type of alert() message popup at the end/outside the ajax call, the e.preventDefault() fires correctly.  I'm sure I'm missing something in relation to how ajax results work with the preventDefault() functionality.  Any help would be most appreciated.

Thank you!

Scott

3 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 06 Apr 2011, 08:20 PM
In reviewing my post I realize it sounds confusing so I hope this helps to clarify the issues:

The bottom line is that the e.preventDefault() event inside the AJAX call seems to fire but does not stop the data from being saved.   Any help or suggestions would be most helpful.

Thanks!
0
Kevin
Top achievements
Rank 1
answered on 20 Sep 2011, 08:00 PM
The prevent default line of code should be before the Ajax call is made.  Once you make an ajax call, it's success part is asynchronous.   
0
Johnny
Top achievements
Rank 2
answered on 08 Jan 2013, 05:25 PM
thanks
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Johnny
Top achievements
Rank 2
Share this question
or