我的代码可以在 Chrome 控制台中获取数据
但无法在我的程序中获取数据
代码如下:
$(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;
}
}
});
});
=====更新===== 如果我移动,响应是正确的
// Create jqxTree
$('#jqxTree').jqxTree({ source: source, theme: theme});
进入成功:函数(响应、状态、xhr){}
菜单显示正确
但是source
变量在外部仍然没有值
====已解决====
for (var menu in source[0])
应该是
for (int i=0;i<source[0].length;i++)
最佳答案
ajax 调用是异步的,因此源变量可能尚未初始化。尝试放置
for(var src in source) {
if (src.items.legth > 0) {
src.expanded = true;
}
}
// Create jqxTree
$('#jqxTree').jqxTree({ source: source, theme: theme});
在 succes 函数中。
关于javascript - ajax获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13064381/