I am retrieving list from ajax, when it bee 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]
I am retrieving list from ajax, when it bee 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
Share Improve this question edited Oct 10, 2017 at 7:07 Liam neesan asked Oct 10, 2017 at 7:01 Liam neesanLiam neesan 2,5719 gold badges38 silver badges89 bronze badges 6I return the
list<string>
from[webmethod]
-
What does you
data
look like ? Tryconsole.log(data)
– abhishekkannojia Commented Oct 10, 2017 at 7:04 -
provide the structure of
data
– Sagar Jajoriya Commented Oct 10, 2017 at 7:04 -
@abhishekkannojia it has a
list<string>
– Liam neesan Commented Oct 10, 2017 at 7:05 - JSON.parse(data).length – krishna raikar Commented Oct 10, 2017 at 7:10
- 1 @mohamedfaiz alert(data.d.length); – krishna raikar Commented Oct 10, 2017 at 7:13
3 Answers
Reset to default 4use 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.