今早有分享一篇《创建Web API并使用http://www.cnblogs.com/insus/p/7771428.html 接下来,我再分享一篇,怎样在angularjs去呼叫Web API。

定义一个mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Insus.NET.Models
{
public class GarbageClassification
{
private string _ClassificationName; public string ClassificationName
{
get { return _ClassificationName; }
set { _ClassificationName = value; }
}
}
}

Source Code

添加一个实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Insus.NET.Models; namespace Insus.NET.Entities
{
public class DataEntity
{
public List<GarbageClassification> GetDatas()
{
return new List<GarbageClassification>(){
new GarbageClassification(){ ClassificationName="废纸"},
new GarbageClassification(){ ClassificationName="塑料"},
new GarbageClassification(){ ClassificationName="玻璃"},
new GarbageClassification(){ ClassificationName="金属物"},
new GarbageClassification(){ ClassificationName="布料"}
};
}
}
}

Source Code

如果你的数据是来自数据库,那你需要修改这个实体类,修改其中的代码,去数据库读取数据。

现在,我们需要创建Web API:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Insus.NET.Models;
using Insus.NET.Entities; namespace Insus.NET.Apis
{
public class GarbageApiController : ApiController
{ [Route("api/GarbageApi/GetClassifications")]
[HttpPost]
public List<GarbageClassification> GetClassifications()
{
DataEntity de = new DataEntity();
return de.GetDatas();
}
}
}

Source Code

OK,model,Entity和API均准备好了。下面是在前端使用angularjs去呼叫刚才写好的API。

Html:

angularjs代码,其实跟jQuery的ajax call差不多一样:

 var oApp = angular.module('GarbageApp', [])
oApp.controller('GarbageCtrl', function ($scope, $http) {
var obj = {}; $http({
method: 'POST',
url: '/api/GarbageApi/GetClassifications',
dataType: 'json',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: JSON.stringify(obj),
}).then(function (response) {
$scope.Classifications = response.data;
});
});

Source Code

演示(运行结果):

angularjs呼叫Web API的更多相关文章

  1. Web API WinForm使用HttpClient呼叫Web API

    前言 之前几篇文章已经介绍了 Web 与 Web API 的使用方式,接下来将介绍如何在 Windows Form 呼叫 Web API 的方法,要在 WinForm 中使用 Web API 的话,除 ...

  2. 动态枢轴网格使用MVC, AngularJS和WEB API 2

    下载shanuAngularMVCPivotGridS.zip - 2.7 MB 介绍 在本文中,我们将详细介绍如何使用AngularJS创建一个简单的MVC Pivot HTML网格.在我之前的文章 ...

  3. [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架

    MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...

  4. 有关AngularJS请求Web API资源的思路

    页面部分大致如下: <body ng-app="productManagement"> ... <div ng-include="'app/produc ...

  5. 从angularjs传递参数至Web API

    昨天分享的博文<angularjs呼叫Web API>http://www.cnblogs.com/insus/p/7772022.html,只是从Entity获取数据,没有进行参数POS ...

  6. 传递多个参数并获取Web API的数据

    近段时间学习Web Api觉得非常有意思.默认的路由情况之下,获取数据时,它不必指定Action操作名. 还有另外感想,就是自从学习asp.net MVC之后,加上jQuery,让Insus.NET已 ...

  7. Web API使用HttpResponseMessage与HttpResponseException的差异 HttpResponseMessage 返回类型

    在 Web API 中提供了 HttpResponseMessage 与 HttpResponseException 用于处理返回讯息,HttpResponseMessage 用于返回一个来自于客户端 ...

  8. 完成AngularJS with MVC 5, Web API 2项目

    经过接近两个月的日夜奋战,完成AngularJS with MVC 5, Web API 2的项目,这也是进入公司以后最大的一个项目,从项目需求.用户Prototype/Demo,招人,开发完成,可谓 ...

  9. [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序

    本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...

随机推荐

  1. 【转】pam_mysql - MySQL error (Can't connect to local MySQL server through socket

    转自:http://350201.blog.51cto.com/340201/1034672 参照 http://wjw7702.blog.51cto.com/5210820/936244博 主做的p ...

  2. 【转载】Android RecyclerView 使用完全解析 体验艺术般的控件

    崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...

  3. 对word2vec的理解及资料整理

    对word2vec的理解及资料整理 无他,在网上看到好多对word2vec的介绍,当然也有写的比较认真的,但是自己学习过程中还是看了好多才明白,这里按照自己整理梳理一下资料,形成提纲以便学习. 介绍较 ...

  4. tkinter中button按钮控件(三)

    button控件 简单的实现: import tkinter wuya = tkinter.Tk() wuya.title("wuya") wuya.geometry(" ...

  5. SQL PLUS的语句执行Commit

    oracle 中有个commit,是用来提交事务的.今天发现sql developer和sql plus的数据查询不一样. 如果我们对数据库进行增删改查,在提交sql语句之后,如果不点击commit, ...

  6. SQL Server自动备份存储过程和视图的方法

    1 建立备份数据表 CREATE TABLE [dbo].[ProcBackup]( ,) NOT NULL, [name] [sysname] NOT NULL, ) NULL, [obj_id] ...

  7. Django电商项目---完成注册页面和用户登录day1

    完成基本的创建项目.用户注册.登录.注销功能 创建Django项目,创建df_user的App 创建静态文件夹static(跟manage.py保持在同一级别下) 复制静态文件(css + image ...

  8. Head First Android --- Intent

    How to create the intentYou create an intent that specifies an action using the following syntax:whe ...

  9. February 6th, 2018 Week 6th Tuesday

    To be is to be perceived. 存在即被感知. How to interpret this quote? Maybe it means that everything in you ...

  10. Gradle的介绍与安装

    Gradle简介 Gradle是一款致力于自动化构建和对多种开发语言的支持的构建工具.如果你想在任意开发平台上构建.测试.发布和部署软件,那么Gradle提供了一个非常灵活的模型,可以支持整个开发生命 ...