My code can get data in Chrome console
but cant get data in my program
code like this:
$(document).ready(function () {
var theme = '';
var source = [];
var rsps = '';
$.ajax({
url: '@Url.Content("~/Home/RoleMenus")',
type: "GET",
cache: false,
success: function(response, status, xhr) {
rsps = response;
source = eval(response);
},
error: function(XMLHttpRequest,textStattus,errorThrown) {
$('#jqxErrorMsg').html(errorThrown);
}
});
for(var src in source) {
if (src.items.legth > 0) {
src.expanded = true;
}
}
// Create jqxTree
$('#jqxTree').jqxTree({ source: source, theme: theme});
$('#jqxTree').bind('select', function (event) {
var args = event.args;
var item = $('#jqxTree').jqxTree('getItem', args.element);
for (var menu in source[0]) {
if (item.label == menu.label) {
window.location = menu.actionUrl;
//break;
}
}
});
});
=====update=====
the response is right, if i move
// Create jqxTree
$('#jqxTree').jqxTree({ source: source, theme: theme});
into success: function(response, status, xhr) {}
the menu shows correctly
but the source variable still has no value outside
=====solved====
for (var menu in source[0])
should be
for (int i=0;i<source[0].length;i++)
The ajax call is async so source variable may not be initialized yet. Try putting
in the succes function.