【问题标题】:Ajax call not getting data which is in json format from APIAjax 调用未从 API 获取 json 格式的数据
【发布时间】:2020-07-14 05:08:15
【问题描述】:

我正在尝试使用下面的 ajax 函数从 news.cshtml 页面的 asp.net core razor 页面调用 api 函数。

它给console.log(len); 提供未定义的错误...如果我删除它,那么什么都不会发生,没有数据,没有错误

 $.ajax({
                url: "api/news/getallnews/1",
                type: "GET",
                dataType: "json",
                success: function (response) {
                    //console.log(response);
                    var len = response.length;
                    console.log(len);
                    var table = $("<table><tr><th>Details</th></tr>");
                    for (var i = 0; i < len; i++) {
                       // console.log("i "+i);
                        table.append("<tr><td>Title:</td><td>" + response[i].newsHeading + "</td></tr>");
                    }
                    table.append("</table>");
                    $("#news").html(table);
                }
            });

通过 api 返回的 json 数据, 我只显示一行,而我的 api 返回大约 36

    {"data":[
    {
    "newsID":2076,
"newsHeading":"New Title is here",
    "newsBrief":"New Brief is here",
    "newsDetails":"\u003Cp\u003E\r\n\t\u003Cem\u003E New details in HTML format are here.\u003C/p\u003E\r\n",
    "newsDate":"2020-01-28T00:00:00",
    "languageID":1
    }]}

【问题讨论】:

    标签: asp.net ajax api asp.net-core


    【解决方案1】:

    试试这个:

    var len = response.data.length;
    console.log(len);
    

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2021-12-07
      • 2018-08-14
      • 2020-09-23
      • 1970-01-01
      相关资源
      最近更新 更多