我一直在試圖從我的web API中獲取信息到我的應用程序中。 目前我只是試圖拉入數據還沒有提交。 API正在我的系統上作爲服務運行和運行。WEB API + ASP.NET嘗試以json格式顯示來自WEB.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的錯誤部分來顯示數據。
在這一點上,我一定在做些傻事,但是不能讓這個部分通過。
您是否嘗試過加入'document.ready'函數內部jQuery代碼之前設置該屬性在你的js文件? – Niklas
試過同樣的問題 – yrthilian