如何在另一个函数中使用ajax的回调函数

时间:2016-09-16 07:46:58

标签: javascript jquery ajax

如何在另一个函数中使用ajax的回调函数

我有ajax的功能:

function correct_date(raw_date){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                showTheValue(result);
            }  
     });
}

    var showTheValue = function(correct_day_value) {
     console.log(new Date(correct_day_value.DATE_NEW).toLocaleDateString('de-DE'));
     return correct_day_value;
    };

我希望在另一个函数中得到ajax的响应/数据值:

function correct_start_date() {
        document.getElementsByTagName("INPUT")[1].value = showTheValue();
    }

如何在另一个函数中使用ajax的响应数据?

5 个答案:

答案 0 :(得分:1)

您必须使用以下两个功能:

function correct_date(raw_date){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                correct_start_date(showTheValue(result));//***
            }  
     });
}

var showTheValue = function(correct_day_value) {
     console.log(new Date(correct_day_value.DATE_NEW).toLocaleDateString('de-DE'));
     return correct_day_value;
};

function correct_start_date(correct_day_value) {
    document.getElementsByTagName("INPUT")[1].value = correct_day_value;
}

或者如果" correct_start_date"根据案例使用:

function correct_date(raw_date){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                var correct_day_value = showTheValue(result);
                if (/* some case */) {
                    correct_start_date(correct_day_value);//***
                }
            }  
     });
}

或等到Ajax设置该值:

var globalVar = null;
function correct_date(raw_date){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                globalVar = showTheValue(result);
                //correct_start_date(globalVar);
            }  
     });
}

var showTheValue = function(correct_day_value) {
     console.log(new Date(correct_day_value.DATE_NEW).toLocaleDateString('de-DE'));
     return correct_day_value;
};

function getGlobalVar() {
    if(globalVar == null) {
        window.setTimeout(getGlobalVar, 50); 
    } else {
        return globalVar;
    }
}

function correct_start_date() {
    if (
    document.getElementsByTagName("INPUT")[1].value = getGlobalVar();
}

答案 1 :(得分:1)

function correct_date(raw_date, callback){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                return callback(result);
            }  
     });
}

function showTheValue() {
    correct_date(raw_date, function(correct_day_value) {
        document.getElementsByTagName("INPUT")[1].value = correct_day_value;            
    });
}

答案 2 :(得分:1)

你可以使用JavaScript Promise。

  

http://www.html5rocks.com/en/tutorials/es6/promises/

function get(url) {
  // Return a new promise.
  return new Promise(function(resolve, reject) {
    // Do the usual XHR stuff
    var req = new XMLHttpRequest();
    req.open('GET', url);

    req.onload = function() {
      // This is called even on 404 etc
      // so check the status
      if (req.status == 200) {
        // Resolve the promise with the response text
        resolve(req.response);
      }
      else {
        // Otherwise reject with the status text
        // which will hopefully be a meaningful error
        reject(Error(req.statusText));
      }
    };

    // Handle network errors
    req.onerror = function() {
      reject(Error("Network Error"));
    };

    // Make the request
    req.send();
  });
}

答案 3 :(得分:0)

此代码对我有用:

function correct_date(raw_date){
        return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                    method: 'correct_date',
                    date: raw_date
            },
            cache: false,
            dataType: 'json'
     });
}

然后我可以将它插入任何我想要的地方:

function parse_correct_day() {
      .
      .
      .
      .  
        var parse_correctday_value = correct_date("12.1.2016");
        parse_correctday_value.success(function (data) {
            var corrected_date = new Date(data.DATE_NEW);
            document.getElementsByTagName("INPUT")[1].value = corrected_date.toLocaleDateString('de-DE');
        });
}

答案 4 :(得分:-1)

您应该从函数showTheValue返回结果,而不是调用2个函数,然后在所需元素中显示响应:

function correct_date(raw_date){ 
    return $.ajax({
            method: "GET",
            url: "../date/correct.php",
            data: {
                method: 'correct_date',
                date: raw_date
            },
            cache: false,
            dataType: 'json',
            success: function(result) {
                console.log(result.DATE_NEW);
                //You need to check the return value of your function and add the value accordingly
                document.getElementsByTagName("INPUT")[1].value = showTheValue(result);
            }  
     });
}

function showTheValue(correct_day_value) {
   var localDate = new Date(correct_day_value.DATE_NEW).toLocaleDateString('de-DE');
   console.log(localDate);
   return localDate;
};
相关问题