locked
How to exit if variable has value? RRS feed

  • Question

  • User1224379429 posted

    Hi

    I'm returning a string from webmethod. If string contains data I should not allow javascript code to execute else continue. I have my below code but it's not working. 

      var isVerified = false;
                $.ajax({
                    //type: 'GET',
                    url: 's.aspx/Verify',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data, status) {
                        debugger;
                        isVerified = true; 
                        alert(data.d);// Here I got data from WebMethod
                    },
                    failure: function (data) {
                        isVerified = false;
                        alert(data.d);
                    },
                    error: function (data) {
                        isVerified = false;
                        alert(data.d);
                    }
                });
                //I want to run the below code only if variable is false
                if (isVerified == false) {
                    debugger;
                    if (chekIdType(id)) {
                        $.LoadingOverlay("show");
                        $("#hdnIdType").val(id.substring(0, 1));
                        var mem = {};
                        mem.RequestId = $("#hdnRequestId").val();                  
                        var depH = $("#txtDepHY").val();
                        if (depH != "") {
                            $("#btnGroupType").val("ED");                       
                        }
                        else {
                            $("#btnGroupType").val("E");
                            member.RequestType = "E";
                        }
    
    
                        $.ajax({
                            type: "POST",
                            url: "s.aspx/Add",
                            data: "{member:" + JSON.stringify(member) + "}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: OnSuccess,
                            failure: function (response) {
                                alert(response.d);
                            },
                            error: function (response) {
                                alert(response.d);
                            }
                        });
    
                        function OnSuccess(response) {
                            var reqDetId = response.d;                     
                        }
                    }
                    else {
                        alert("invalid id format");
                    }
                }

    Thursday, September 20, 2018 6:38 AM

Answers

  • User1724605321 posted

    Hi Aristocrat Khan,

    Then you should move that part of codes into the success callback of ajax :

      success: function (data, status) {
                        debugger;
                        isVerified = true; 
                        alert(data.d);// Here I got data from WebMethod
    
                        //Your javascript function 
    
                        if (isVerified == false) {
                        debugger;
                        if (chekIdType(id)) {
                        $.LoadingOverlay("show");
    
                        ...
                    },

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2018 8:47 AM

All replies

  • User1724605321 posted

    Hi Aristocrat Khan ,

    Athar Ali Khan

    I'm returning a string from webmethod. If string contains data I should not allow javascript code to execute else continue.

    If you want to know whether return string is not empty . You can try :

     if (data.d) {
        //write your logic 
        alert("Has value")
     }

    And you should move your logic inside the callback function of Ajax . For example :

                var isVerified = false;
                $.ajax({
                    //type: 'GET',
                    url: 's.aspx/Verify',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data, status) {
                        debugger;
                        isVerified = true; 
                        alert(data.d);// Here I got data from WebMethod
                        if(data.d){
    
                         myloic();
                        }
                       
                    },
                    failure: function (data) {
                        isVerified = false;
                        alert(data.d);
                    },
                    error: function (data) {
                        isVerified = false;
                        alert(data.d);
                    }
                });
                
                function myloic()
                {
                    //I want to run the below code only if variable is false
                    if (isVerified == false) {
                    debugger;
                    if (chekIdType(id)) {
                        $.LoadingOverlay("show");
                        $("#hdnIdType").val(id.substring(0, 1));
                        var mem = {};
                        mem.RequestId = $("#hdnRequestId").val();                  
                        var depH = $("#txtDepHY").val();
                        if (depH != "") {
                            $("#btnGroupType").val("ED");                       
                        }
                        else {
                            $("#btnGroupType").val("E");
                            member.RequestType = "E";
                        }
    
    
                        $.ajax({
                            type: "POST",
                            url: "s.aspx/Add",
                            data: "{member:" + JSON.stringify(member) + "}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: OnSuccess,
                            failure: function (response) {
                                alert(response.d);
                            },
                            error: function (response) {
                                alert(response.d);
                            }
                        });
    
                        function OnSuccess(response) {
                            var reqDetId = response.d;                     
                        }
                    }
                    else {
                        alert("invalid id format");
                    }
                   }
    
                }

    You can modify the code base on your own logic &requirement .

    Best Regards,

    Nan Yu

    Thursday, September 20, 2018 7:09 AM
  • User1224379429 posted

    HI

    I'm getting the value from Webmethod, that's not my issue.

    First I want to execute the below code. If string is empty then continue else exit
    var isVerified = false; $.ajax({ //type: 'GET', url: 's.aspx/Verify', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, status) { debugger; isVerified = true; alert(data.d);// Here I got data from WebMethod }, failure: function (data) { isVerified = false; alert(data.d); }, error: function (data) { isVerified = false; alert(data.d); } }); //I want to execute the below code only if variable isVerified is false, but what's happening is, after executing the below code, isVerified is being set as true. if (isVerified == false) { debugger; if (chekIdType(id)) { $.LoadingOverlay("show");
    Thursday, September 20, 2018 7:32 AM
  • User1724605321 posted

    Hi Aristocrat Khan,

    Your are using Ajax to make asynchronous request , after success callback function fires , the "isVerified " is changed to True . Could you please explain more about  your requirement , and do you put if (isVerified == false) {} logic in same function with Ajax above ? 

    Best Regards,

    Nan Yu

    Thursday, September 20, 2018 7:38 AM
  • User1224379429 posted

    HI

    wait for ajax to finish webmethod 's.aspx/Verify' and then continue javascript

    I want to make wait till the below method gets finished. 
    var isVerified = false; $.ajax({ type: 'GET', url: 's.aspx/Verify', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data, status) { debugger; isVerified = true; alert(data.d);// Here I got data from WebMethod }, failure: function (data) { isVerified = false; alert(data.d); }, error: function (data) { isVerified = false; alert(data.d); } });

    //Once above method is completed, execute the below code if (isVerified == false) { debugger; if (chekIdType(id)) { $.LoadingOverlay("show");
    Thursday, September 20, 2018 8:43 AM
  • User1724605321 posted

    Hi Aristocrat Khan,

    Then you should move that part of codes into the success callback of ajax :

      success: function (data, status) {
                        debugger;
                        isVerified = true; 
                        alert(data.d);// Here I got data from WebMethod
    
                        //Your javascript function 
    
                        if (isVerified == false) {
                        debugger;
                        if (chekIdType(id)) {
                        $.LoadingOverlay("show");
    
                        ...
                    },

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2018 8:47 AM
  • User1224379429 posted

    HI Nan,

    Thanks man!

    Thursday, September 20, 2018 9:44 AM