0

I get data from a $.ajax call but cant work correctly with the data of it. 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. My metaName has all attribute values for my element from recordset. I fill also my metaName array with a $.ajax call. I dont find my mistake. :/

4

1 回答 1

4

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

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');
}
于 2013-07-17T11:44:29.450 回答