简体   繁体   English

jQuery无法正确处理$ .ajax中的数据

[英]jQuery Can't work with data from $.ajax correctly

I get data from a $.ajax call but cant work correctly with the data of it. 我从$ .ajax调用中获取了数据,但无法正确处理其数据。 Here is my code. 这是我的代码。

function OnSuccessResultSet(data, status) {
            var output;
            for (var i in data.recordset) {
                output += "<li><a href='#'>";
                for (var j = 0; j < metaName.length; j++) {
                    var testVar = metaName[j];
                    output += " <h2>" + data.recordset[i].testVar+ "</h2>";
                    alert(data.recordset[i].testVar);
                    alert(testVar);
                    alert(data.recordset[i].LABEL);
                };
                output += "</a></li>";
            }
            $(output).appendTo("#content1");
            $('#content1').listview('refresh');
        }

The first alert gives me an undefined back. 第一个警报给了我不确定的回复。 Second alert gives me LABEL back and third one gives me the value of LABEL. 第二个警报给了我LABEL,第三个警报给了我LABEL的价值。 My metaName has all attribute values for my element from recordset. 我的metaName具有记录集中元素的所有属性值。 I fill also my metaName array with a $.ajax call. 我也用$ .ajax调用填充了我的metaName数组。 I dont find my mistake. 我没有发现我的错误。 :/ :/

I think you need to use bracket notation instead of dot notation as the member operator here as the key you are looking for is stored in the variable testVar 我认为您需要使用方括号表示法而不是点表示法,因为您要查找的键存储在变量testVar ,因此这里的成员运算符

alert(data.recordset[i][testVar]);

Ex 防爆

function OnSuccessResultSet(data, status) {
    var output, testVar;
    for (var i in data.recordset) {
        output += "<li><a href='#'>";
        for (var j = 0; j < metaName.length; j++) {
            testVar = metaName[j];
            output += " <h2>" + data.recordset[i][testVar]+ "</h2>";
        };
        output += "</a></li>";
    }
    $(output).appendTo("#content1");
    $('#content1').listview('refresh');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM