Home » Jquery » javascript – Problem in getting a return value from an ajax script

javascript – Problem in getting a return value from an ajax script

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am new to JavaScript and AJAX. In ‘Change Password’ form I want to verify the ‘Current Password’ using an AJAX function. I have wrote the function after reading some of the answers from stackOverflow. But I have to call the function (Submit button) for 2 time to get the return value.

How to get the return value at first occurrence?
My Code is:

   var validCurrentPass;

   var setTrue=function (returned_Data){ validCurrentPass=returned_Data;
     };

function isRecordExist(tableName,Fields,wFields,wValues,MsgID,phpFile,callBack) {
  var returned_Data;
  var params='';
  var http = new XMLHttpRequest();
  var url = 'ajax_files/'+phpFile;
  params='tableName='+tableName;
  params=params+'&fields='+Fields;
  params=params+'&wherefields='+wFields;
  params=params+'&wherevalues='+wValues;

  http.open('POST', url, true);

  //Send the proper header information along with the request
  http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

  http.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
				 returned_Data=this.responseText;
				callBack.apply(this,[returned_Data]);
            }
 };
	
http.send(params);

}

and I call the function like this:

isRecordExist(table,"username",wFields,wValues,"txtNewMsg","ajaxFind.php",setTrue);
How to&Answer: