我制作了一个Web api并将其发布到localhost。
创建了一个使用AngularJS制作脚本文件的网页。
api和网页都在localhost中运行。
当我发送http get请求时,我得到一个JSON响应,但无法使用Post方法向api添加数据。
帖子请求完美地适用于Postman。它增加了新的数据。
app.js
var app = angular.module("myApp", []);
app.controller('myCtrl',function($scope,$http){
$scope.register = function () {
$http({
method: "POST",
url: 'http://localhost:8080/pool/api/employees/',
headers: { 'Content-Type': 'application/json' },
data:JSON.stringify({"PSID": "1236","Name": "Michael","Type": "Owner","Ph": "9585456211"})
});
}
$scope.search = function () {
url = 'http://localhost:8080/pool/api/employees/' + $scope.getid;
$http.get(url).success(function (data) {
$scope.msg = "";
alert(JSON.stringify(data));
$scope.id = data.PSID;
$scope.name = data.Name;
$scope.type = data.Type;
$scope.phone = data.Ph;
}).
error(function (data) {
$scope.msg = "No PSID found";
$scope.id = "";
$scope.name = "";
$scope.type = "";
$scope.phone = "";
})
}
});
Web Api控制器代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using webapi.Models;
namespace webapi.Controllers
{
public class EmployeesController : ApiController
{
private webapiContext db = new webapiContext();
// GET: api/Employees
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
// GET: api/Employees/5
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> GetEmployee(string id)
{
Employee employee = await db.Employees.FindAsync(id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
// PUT: api/Employees/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutEmployee(string id, Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != employee.PSID)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Employees
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (EmployeeExists(employee.PSID))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = employee.PSID }, employee);
}
// DELETE: api/Employees/5
[ResponseType(typeof(Employee))]
public async Task<IHttpActionResult> DeleteEmployee(string id)
{
Employee employee = await db.Employees.FindAsync(id);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
await db.SaveChangesAsync();
return Ok(employee);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(string id)
{
return db.Employees.Count(e => e.PSID == id) > 0;
}
}
}