0

関数の外でwindows.urisを宣言し、window.urisが空の場合の最後の行で、$。each()に要素を追加しています。なんで ?

window.uris = new Array(); 

window.groups = new Array();

jQuery(document).ready(function($) {


host = document.location.host,
path = document.location.pathname;
url = host + path;
var domg = new Object();
var optgroup;
var productsDom = "";

  if (contains(url, 'manage/items.php')) {
    $.post('http://localhost/frontaccounting/proxy.php',
    {
        "url":"http://localhost:8081/stock/categories/",
        "m":"get",
        "data": ""
    },
      function (data) {
        d = $.parseXML(data);
        $xml = $( d );

        $xml.find("category").each(
            function (i,e) {
                optgroup = '<optgroup label="'+ $(e).children("name").text()+'">';
                categoryId = $(e).children("id").text();
                auxx =  (categoryId*1);
                window.uris[i]="http://localhost:8081/stock/categories/" + (auxx );
                window.groups[auxx + ""] = optgroup;
            }
        );
    }
    );  
    //sleep(2000);
        alert('URI' + window.uris);
4

1 に答える 1

2

関数では、いくつかのURLを非同期で呼び出します。ただし、alertコマンドは関数内にあり、コールバック内にはありません。

そのため、AJAXリクエストが終了してデータが存在するまで待つのではなく、すぐに呼び出されます。

簡単な解決策はalert、コールバック関数内に移動することです。

于 2012-05-14T11:33:14.243 に答える