最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find the length of Success Data in Jquery Ajax? - Stack Overflow

programmeradmin2浏览0评论

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

I return the list<string> from [webmethod]

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 6
  • What does you data look like ? Try console.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
 |  Show 1 more ment

3 Answers 3

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论