Friday, 27 September 2013

jquery ajax call success, how do i change a global variable in the JavaScript function?

Sometimes we have problems on jquery success function.

we are assign a global variable value within the sucess function,
now we want to access that variable, we will see that variable till now hold the 1st value not taking the assign value.
How We solve the problem...

var strValue  =null;
function validateEmail() {
var obj = {
          emailText: $('#txtemail').val(),
          int_mode: 2
        };

$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "webservice/remote_service.asmx/webserviceTestEmail",
dataType: "json",
        data: "JSON.stringify(obj),
        success: onSuccess,
        error: function(result) {
           alert("Error");
        }
      });
}

function onSuccess {
    if (result.strControlerMessage == 'n') {
      strValue = "n";
    }
    else {
      strValue = "y";
    }
}
function validateForm(){
  validateEmail();
 
  alert(strValue); /////we will see its return ""
 
}
//////////////////////////////////////////////////////////////
Solution Of this problem 
use  cache: false,
     async: false,
before jquery success

$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "webservice/remote_service.asmx/webserviceTestEmail",
dataType: "json",
cache: false,
        async: false,
        data: "JSON.stringify(obj),
        success: onSuccess ,
        error: function(result) {
         alert("Error");
        }
      });

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...