WEB API + ASP.NET尝试以json格式显示来自WEB.API的数据

yrthilian

我一直在尝试将信息从Web API提取到应用程序中。目前,我只是想获取尚未提交的数据。该API在我的系统上作为服务正在运行并正在运行。

它以json格式返回数据,这是WEB API返回的数据的示例。

    [
  {
    "$id": "1",
    "id": "6c32e378-0f06-45da-9dda-0515c813cd5d",
    "application_name": "FDB INTERFACE",
    "description": "Interface for FDB decision support",
    "active": true,
    "tbl_update_header": []
  },
  {
    "$id": "2",
    "id": "58f68624-3803-43ff-b866-0a507ea85459",
    "application_name": "HPM",
    "description": "Helix Health Practice Manager",
    "active": true,
    "tbl_update_header": []
  },

这是我的页面,只是为了尝试获取一些要显示的数据

        <html>
<head>
    <title></title>
    <script src="~/Scripts/jquery-2.1.1.js"></script>
    <script type="text/javascript">
        $.ajax({
            type: "GET",
            url: "http://localhost:9981/API/Application",
            processData: true,
            data: {},
            dataType: "json",
            error: function (jqXHR, textStatus, errorThrown) {
                // debug here
                alert(jqXHR);
            },
            //error: function(error, data){
            //    console.log(error)
            //},
            success: function (data) {
                //Clear the div displaying the results
                $("productView").empty();
                //Create a table and append the table body
                var $table = $('<table border="2">');
                var $tbody = $table.append('<tbody />').children('tbody');
                //data - return value from the web api method
                for (var i = 0; i < data.lenght; i++) {
                    //adda new row to the table
                    var $trow = $tbody.append('<tr />').children('tr:last');
                    //add a new column to display name
                    var $tcol = $trow.append('<td/>').children('td:last').append(data[i].id);
                    //add a new column to display description
                    var $tcol = $trow.append('<td/>').children('td:last').append(data[i].description);
                }
                //display the table in the div
                $table.appendTo('#productView');
            }
        });
    </script>

</head>
<body>
    <div id="productView"></div>
</body>
</html>

页面已加载,但为空,任何部分均未返回任何错误。我从chrome / FF / IE运行网页,但在开发模式下它们均未显示错误,而VS未显示任何错误。我不确定我是错误地解析数据还是调用json的错误部分来显示数据。

在这一点上,我必须做些愚蠢的事情,但是无法通过这部分。

钱德里卡(Chandrika Prajapati)

您也可以在ajax调用之前在js文件中设置此属性

$.support.cors = true;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章