简体   繁体   中英

How to find the length of Success Data in Jquery Ajax?

I am retrieving list from ajax, when it become success, I wants to add those list values to DropDownList Items, So I want to iterate the data until last value and then add it to DropDownList

Here is my code What i tried

$.ajax({

    type: "POST",
    contentType: "application/json;charset=utf-8",
    url: "GPCreateCheque.aspx/getOpenRequestNo",
    dataType: "json",
    success: function (data) {
       alert(data.length);

        for (var i = 0; i<data.length; i++) {
            $(".tbDDLReqNo").append(new Option(data.d[i], data.d[i]));
        }
    },
    error: function (result) {
        alert("Error");
    }

})

In alert box it is showing undefined

UPDATE

I return the list<string> from [webmethod]

use data.d.length

alert(data.d.length);

for (var i = 0; i<data.d.length; i++) {
    $(".tbDDLReqNo").append(new Option(data.d[i], data.d[i]));
}

You need to do data = JSON.parse(data); before treating your data like an array. It's just a string up to that point.

Update: if your data is not the array itself, but instead an object that contains an array named d , you need to work with that. It depends on your data structure.

Check the response from network tab in Chrome Developer Console or similar tool. May be response code is 200 but response data is missing.

On the other hand you want to itarete data in for loop but you try to append data.d[i] . If you want to append data.d[i] you have to itarete data.d in for loop.

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