How to stop execution inside ajax success function

#1

I’ve the following Ajax call and I am having problem when result.trim() == ''.

Whenever there is a database related error, I get result.trim() == '' and I can see the alert message window saying Data Unavailable. But after this, I don’t want to execute the rest of code.
Because, in the line below, I have this line defined $("#description").html(gridDescription); which keeps on executing even if this result.trim() == ''. is true. I added the else condition as shown below
and it didn’t work.

How can I stop the execution of $("#description").html(gridDescription); when I am inside this if statement

 if (result.trim() == '') {
                            dalert.alert("No data available for display!", "Data Unavailable!");
                           
                     }
$.ajax({
                url: url + "?dataID=" + refdata,
                success: function(result) {
                    
                    if (result.trim() == '') {
                            dalert.alert("No data available for display!", "Data Unavailable!");
                           
                     }
                    else{

                    var parsedResult = JSON.parse(result);
                  

                    $('#data_dialog').dialog({
                        autoOpen: false,
                                         
                        maxHeight: 500,
                        width: 1000,
                        height: 700,
                        modal: true,
                        buttons: {
                            "Close": function() {
                                $(this).dialog("close");
                                                              
                            },
                            Download: function() {
                                $("#jQXgrid").jqxGrid('exportdata', 'xls', gridDescription);

                             },
                            Print: function() {
                                    var gridContent = $("#jQXgrid").jqxGrid('exportdata', 'html');
                                    var newWindow = window.open('', '', 'width=800, height=500'),
                                                document = newWindow.document.open(),
                                                pageContent =
                                                    '<!DOCTYPE html>\n' +
                                                    '<html>\n' +
                                                    '<head>\n' +
                                                    '<meta charset="utf-8" />\n' +
                                                    '<title>jQWidgets Grid</title>\n' +
                                                    '</head>\n' +
                                                    '<body>\n' + gridContent + '\n</body>\n</html>';
                                                document.write(pageContent);
                                                document.close();
                                                newWindow.print();

                                                             }
                                                        }
                    });

                    
                    $('#data_dialog').dialog('open');

                    var column = new Array();
                    var data = "";
                    $.each(Object.keys(parsedResult[1]), function(k, v) {
                        data += k + " , " + v + "\n";
                        column.push({
                            datafield: v,
                            align:'center',
                            cellsalign: 'center'
                        });

                    });

                    

                    var localData = new Array();

                    // prepare the data
                    var source = {
                        datatype: "json",
                        localdata: Object.values(parsedResult)
                    };
                    var dataAdapter = new $.jqx.dataAdapter(source);
                                      

                    $("#title").html(titleToPass);
                    $("#description").html(gridDescription);

                    $("#jQXgrid").jqxGrid({
                        width: 670,
                        source: dataAdapter,
                        columnsresize: true,
                        columns: column,
                        sortable: true,
                        pageable: true

                    });

                   }// End of Else


                },

                error: function(request, status, error) {
                    console.log("Inside Error function:");
                    console.log(request.responseText);
                }



            });
0 Likes

#2

Can you use return ?

0 Likes

#3

I should clarify. You can return from the if statement, removing the need for the else clause. I find that I prefer that technique as it results in less nested code, and less nesting is less confusing.

if (condition) {
    console.log(...);
    return;
}
// the rest of the function code
0 Likes

closed #4

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.

0 Likes