AJAX函数最后运行,如何使其先运行

时间:2018-10-21 01:28:45

标签: javascript jquery ajax xml

我使用AJAX调用(以下代码)有一个功能:

function get_type(Name) {
  var field_name;
  $.ajax({
    type: "GET",
    url: "../home/Asset/MR/MR.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('pub').each(function() {
        if (Name == $(this).find('pro').text()) {
          $(this).find('metadata field').each(function() {
            field_name = $(this).find('name').text();

            if (field_name == "little") {
              type = "L";
            } else if (field_name == "Big") {
              type = "b";
            }
          });
        }
      });
    }
  });
}

此代码运行良好,但问题是在所有功能完成后才运行。我想先运行此代码,我需要从XML获取数据。一旦$(xml).find('pub').each(function()文本匹配,就需要停止Name== $(this).find('pro').text()的循环。因为即使执行此循环,我也得到了答案。

呼叫功能代码:

var rd = new FileReader();
rd.onload = function(e) {
  var xmlDoc = $.parseXML(this.result);
  var $xml = $(xmlDoc);
  var J_Name = $xml.find('meta').text();
  get_type(J_Name);
  //check allowed child of front tag
  check_allowed_direct_child("places", "Tirunelveli,Tiruchendur,Alwar", "RULE_002", "Fail");
};
rd.readAsText(this.files[i]);

1 个答案:

答案 0 :(得分:2)

呼救!

function get_type(name, cb) {
    cb = cb || function () {};

    var field_name;
    var type;
    var types_map = {
        'little': 'L',
        'Big': 'b'
    };

    $.ajax({
        type: 'GET',
        url: '../home/Asset/MR/MR.xml',
        dataType: 'xml',
        success: function (xml) {
            $(xml)
                .find('pub')
                .each(function () {
                    if (name == $(this).find('pro').text()) {
                        $(this)
                            .find('metadata field')
                            .each(function () {
                                field_name = $(this)
                                    .find('name')
                                    .text();

                                if (types_map.hasOwnProperty(field_name)) {
                                    type = types_map[field_name];
                                    return false; // break out of each()
                                }
                            });
                        return false; // break out of each()
                    }
                });

            cb(type); // execute provided callback
        }
    });
}

var rd = new FileReader();
rd.onload = function (e) {
    var xmlDoc = $.parseXML(this.result);
    var $xml = $(xmlDoc);
    var J_Name = $xml.find('meta').text();
    get_type(J_Name, function (type) {

        // do stuff once get_type() resolves, type being either matched type or undefined

        check_allowed_direct_child('places', 'Tirunelveli,Tiruchendur,Alwar', 'RULE_002', 'Fail');
    });
};
rd.readAsText(this.files[i]);

如果有兴趣,请阅读如何使用Promises,以使回调代码更具摘要性:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

如果仍然感兴趣,请阅读如何使用异步/等待来使Promises代码更加摘要:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function