How to stop execution inside ajax success function

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!");
                url: url + "?dataID=" + refdata,
                success: function(result) {
                    if (result.trim() == '') {
                            dalert.alert("No data available for display!", "Data Unavailable!");

                    var parsedResult = JSON.parse(result);

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

                            Print: function() {
                                    var gridContent = $("#jQXgrid").jqxGrid('exportdata', 'html');
                                    var newWindow ='', '', 'width=800, height=500'),
                                                document =,
                                                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>';



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



                    var localData = new Array();

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


                        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:");


Can you use return ?

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) {
// the rest of the function code

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