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;           
}
8
  • @AmitJoki note the async: false in the $.ajax() call ... Commented Mar 27, 2015 at 6:14
  • @Pointy it's no longer supported. It's been deprecated, unless OP is using a really old version of jQuery. Commented Mar 27, 2015 at 6: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 :) Commented Mar 27, 2015 at 6: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. Commented Mar 27, 2015 at 6:18
  • 1
    Why don't you move your logic or whatever it has to run inside the success function? Commented Mar 27, 2015 at 6:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.