Angularjs calls Web APIs and angularjs calls APIs

Source: Internet
Author: User

Angularjs calls Web APIs and angularjs calls APIs

This morning I share an article "Create Web API and use" http://www.cnblogs.com/insus/p/7771428.html next, I will share another article, how to call Web API in angularjs.

Define a 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


Add an object class:


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 <attributes> () {new GarbageClassification () {ClassificationName = "Waste Paper"}, new GarbageClassification () {ClassificationName = "plastic"}, new GarbageClassification () {ClassificationName = "Glass"}, new GarbageClassification () {ClassificationName = "metallic"}, new GarbageClassification () {ClassificationName = "fabric "}};}}}Source Code

 

If your data comes from a database, you need to modify the entity class, modify the code, and read data from the database.


Now, we need to create a 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 <criteria> GetClassifications () {DataEntity de = new DataEntity (); return de. getDatas ();}}}Source Code


OK, model, Entity, and API are all ready. The following shows how to use angularjs on the front end to call the previously written API.


Html:


 
Angularjs code is similar to jQuery's ajax call:


Var oApp = angular. module ('garbageapp', []) oApp. controller ('garbagectrl', function ($ scope, $ http) {var obj ={}; $ http ({method: 'post', url: '/api/GarbageApi/GetClassifications', ype: 'json', headers: {'content-type': 'application/json; charset = UTF-8 '}, data: json. stringify (obj ),}). then (function (response) {$ scope. classifications = response. data ;});});Source Code


Demo (running result ):

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.