简体   繁体   中英

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. 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. :/

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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