Jethi Mayank Jethi Mayank - 21 days ago 4
jQuery Question

How to callback user define function and display value

hello everyone i have one function that execute ajax request and result will return to another function but when i call that function it will return undefined value please help !


function getState(id)

var StateJsonString = jQuery.parseJSON(id);
var stateData;


type: "POST",

url: "phpFile/getStatenameVehiclePro.php",

dataType: "json",

data: { id:StateJsonString.state},

success: function(stateFrom){

stateData =[0][1];

Fail: function(){

alert("Cannot Loading State Name");

error: function(){

alert("Cannot Loading State Name");


return stateData;


Add async option to false and return outside the ajax call.

  • Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called.
  • If you set async: true then that statement will begin it's execution and the next statement will be called regardless of whether the async statement has completed yet.


function getState() {
    var result="";
      async: false,  
      success:function(data) {
         result = data; 
   return result;


Handle ajax request

//Ajax function call
function getState() {
  return $.ajax({
      url: "URL"

//Get ajax request
var promise = getState();

//Call on ajax success
promise.success(function (data) {
  //Do what you want