因爲我們正在使用API
除非有人願意使用ReactJs
或AngularJs
,否則不需要創建複雜的基礎架構/體系結構。 對於他們來說,它甚至不是強制性的,但它可以讓你保持代碼清潔。您只需簡單地使用JavaScript調用API
即可 例如,您有一個API控制器方法並希望訪問它。
產品
#region Get Method
// GET api/product
[Queryable]
[Route("Products")]
[Route("All")]
public HttpResponseMessage Get()
{
try
{
var products = _productServices.GetAllProducts();
var productEntities = products as List<ProductEntity> ?? products.ToList();
if (productEntities.Any())
{
return Request.CreateResponse(HttpStatusCode.OK, productEntities);
}
throw new ApiDataException(1000,"No Products Found",HttpStatusCode.NotFound);
}
catch (Exception)
{
throw;
}
}
#endregion
簡單方法只需調用尊重方法從阿賈克斯這樣
$(document).ready(function() {
var a = "Test";
$.ajax({
url: "v1/products/product/All",
type: "GET",
data: { a : a },
success: function (response) {
alert(response);
},
error: function (response) {
alert(response);
}
});});
希望這有助於
使用角度JS或jQuery的通過AJAX來獲取數據。服務器初始頁面爲靜態html文件。 – zaitsman