0

I have two java-script function. In one function i have called another function. Its working fine. But the problem is first function not getting response from second java script. I have requirement if the first function return true then second will execute rest code.

My Code.

function NextData() {           
        var lQIndex = $('#hfLastQIndex').val();  
        if (SaveData(lQIndex)) {
            alert('1111111');
            //Rest Code....
        }            
}

function SaveData(QNo) {   
        var flag = false;
         $.ajax({
            url: '/Assessment/Save',
            type: 'POST',
            data: {A:QNo},
            async: false,
            success: function (data) {
                //alert("Test: " + data.result);
                if (data.result == "T")
                    flag = true;                   
            },
            error: function (req, status, error) {
                //alert("R: " + req + " S: " + status + " E: " + error);
                alert('Unable to connect server!');   
                return false;                 
            }
        }); 
       return flag;           
}
Shehary
  • 9,926
  • 10
  • 42
  • 71
Raghubar
  • 2,768
  • 1
  • 21
  • 31
  • @AmitJoki note the `async: false` in the `$.ajax()` call ... – Pointy Mar 27 '15 at 06:14
  • @Pointy it's no longer supported. It's been deprecated, unless OP is using a really old version of jQuery. – Amit Joki Mar 27 '15 at 06:15
  • @AmitJoki I know it's been deprecated, but I *think* it still works in current browsers. Obviously if you're right about it not working then you're right about this being a duplicate :) – Pointy Mar 27 '15 at 06:16
  • The docs (jQuery docs) say that it's deprecated but not that it doesn't work (except for cross-domain requests and JSONP of course). Also, obviously the OP *shouldn't* be doing a synchronous request because it's a terrible idea. – Pointy Mar 27 '15 at 06:18
  • Yup. You're right. OP should clarify more. – Amit Joki Mar 27 '15 at 06:19
  • I don't think this is the right way to approach the problem. – Quannt Mar 27 '15 at 06:19
  • hey guys so there is no solution for it. – Raghubar Mar 27 '15 at 06:19
  • 1
    Why don't you move your logic or whatever it has to run inside the success function? – Quannt Mar 27 '15 at 06:21

2 Answers2

2

Using Callback functions, you can achieve this easily.

   function NextData() {           
            var lQIndex = $('#hfLastQIndex').val();  
            SaveData(lQIndex,function(result){
                   //rest code----
                   //result will be either true/false
            });
    }

    function SaveData(QNo, successCallback) {   
            var flag = false;
             $.ajax({
                url: '/Assessment/Save',
                type: 'POST',
                data: {A:QNo},
                async: false,
                success: function (data) {
                    //alert("Test: " + data.result);
                    if (data.result == "T")
                        flag = true;  
                      successCallback(flag);
                },
                error: function (req, status, error) {
                    //alert("R: " + req + " S: " + status + " E: " + error);
                    alert('Unable to connect server!'); 
                   flag = false;  
                    successCallback(flag);                 
                }
            }); 
           return flag;           
        }

http://gireeshsb.blogspot.in/2013/08/callback-functions-in-javascript.html http://issacjk.blogspot.in/2014/08/callback-functions-in-javascript.html

Gireesh SB
  • 106
  • 1
  • 1
  • 8
1

Why you not use DataType JSON?

function NextData() {           
    var lQIndex = $('#hfLastQIndex').val();  
    if (SaveData()) {
        alert('1111111');
        //Rest Code....
    }            
}

function SaveData(QNo) { 
    var flag = false;
    var res = $.ajax({
        url: 'save.php',
        type: 'POST',
        data: { A : QNo },
        dataType: 'json',
        async: false,
        success: function (data) {
            console.log(data.result);
            //alert("Test: " + data.result);
            if (data.result == "T")
                flag = true;                   
        },
        error: function (req, status, error) {
            //alert("R: " + req + " S: " + status + " E: " + error);
            alert('Unable to connect server!');   
            return false;                 
        }
    }); 
    if(res){
        return flag;   
    }      
}

it's work for me..

Wahyu Kodar
  • 654
  • 1
  • 6
  • 15