我一直在尝试从我的网络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的错误部分来显示数据。
我现在必须做些傻事,但是不能通过这部分。
答案 0 :(得分:1)
你的成功方法有一个错字......
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.length; 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].application_name);
//add a new column to display description
var $tcol = $trow.append('<td/>').children('td:last').append(data[i].description);
//add a new column to display active
var $tcol = $trow.append('<td/>').children('td:last').append(data[i].active);
}
//display the table in the div
$table.appendTo('#productView');
应该是data.length
而不是data.lenght
。
答案 1 :(得分:1)
成功。问题出在CORS上。还有data.length的拼写错误 代码工作正常,结果是我想要得到的。谢谢大家的帮助。
要解决此问题,我必须在iis服务器端启用CORS以允许跨域访问。
答案 2 :(得分:1)
您还可以在ajax调用之前在js文件中设置此属性
$.support.cors = true;
答案 3 :(得分:0)
上面的代码有一个成功函数,但没有错误函数。
尝试设置错误功能,然后看看会发生什么:
data: {},
dataType: "json",
error: function(jqXHR, textStatus, errorThrown ) {
// debug here
alert(jqXHR);
},
success: function() ...