我制作了一个ASP.NET WEB API,它可以获取客户的详细信息。 json格式的营养素。我在IIS上部署了我的API&它工作正常。现在我想在我的MVC项目中使用该API的URL。我的URL就像这样的“http:/ localhost:12 / customernutrient / customernutrient?o = json”,类就像这样
public class Header
{
public string api_ver { get; set; }
public int req_type { get; set; }
public int code { get; set; }
public string description { get; set; }
}
public class Customer
{
public int customerId { get; set; }
public string customerName { get; set; }
}
public class Nutrient
{
public int nutrientId { get; set; }
public string nutrientName { get; set;}
}
public class Body
{
public List<Customer> customer { get; set; }
public List<Nutrient> nutrient { get; set; }
}
public class RootObject
{
public Header header { get; set; }
public Body body { get; set; }
}
如何借助我的链接调用我的API&amp;在标签或其他东西中显示内容。我想以不同的标签显示这些数据
{
"header": {
"api_ver": "1.2",
"req_type": 1,
"code": 1,
"description": "Successful output"
},
"body": {
"customer": [
{
"customerId": 1,
"customerName": "Roundys1"
},
{
"customerId": 2049,
"customerName": "Test"
}
],
"nutrient": [
{
"nutrientId": 1,
"nutrientName": "Calcium"
},
{
"nutrientId": 2,
"nutrientName": "Calories"
},
{
"nutrientId": 3,
"nutrientName": "Cholesterol"
},
{
"nutrientId": 4,
"nutrientName": "Dietary Fiber"
},
{
"nutrientId": 5,
"nutrientName": "Iron"
},
{
"nutrientId": 6,
"nutrientName": "Polyunsaturated Fat"
},
{
"nutrientId": 7,
"nutrientName": "Potassium"
},
{
"nutrientId": 8,
"nutrientName": "Protein"
},
{
"nutrientId": 9,
"nutrientName": "Saturated Fat"
},
{
"nutrientId": 10,
"nutrientName": "Sodium"
},
{
"nutrientId": 11,
"nutrientName": "Sugars"
},
{
"nutrientId": 12,
"nutrientName": "Total Carbohydrate"
},
{
"nutrientId": 13,
"nutrientName": "Total Fat"
},
{
"nutrientId": 14,
"nutrientName": "Vitamin A"
},
{
"nutrientId": 15,
"nutrientName": "Vitamin C"
}
]
}
}
请分享您可能的解决方案
答案 0 :(得分:0)
您可以使用ajax调用来填充视图。假设您想要在点击id =&#39; btnpopulatedata&#39;的按钮时填充数据。
<script>
$(document).ready(function () {
var itemName = "#btnpopulatedata";
$(itemName).click(function () {
$.ajax({
type: 'GET',
dataType: 'Json',
url: 'http:/localhost:12/customernutrient/customernutrient?o=json',
success: function (data) {
//write your logic to parse Json and populate the view controls.
},
error: function () {
alert("Error);
},
});
});
});
</script>