[英]Why cant i access window.variable in Javascript?
我在任何函数之外声明windows.uris,然后我在$ .each()中添加元素,在警告window.uris为空时在最后一行。 为什么?
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);
在您的函数中,您对某个URL进行异步调用。 但是, alert
命令位于函数内部, 而不在回调内部。
所以它将被立即调用,而不是等到AJAX请求完成并且您的数据存在。
一个简单的解决方案是在回调函数中移动alert
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.