0
我做了一個Web API並將其發佈到本地主機。http發佈請求不能在angularjs中工作,而http獲取作品
在使用AngularJS創建腳本文件的地方創建了一個網頁。
api和網頁都在localhost中運行。
當我發送http獲取請求時,我得到一個JSON響應,但不能使用Post方法將數據添加到api。
郵政請求完美地運作在郵遞員。它增加了新的數據。
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 = "";
})
}
});
網絡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;
}
}
}
在瀏覽器控制檯中是否有任何錯誤?沒有任何錯誤或代碼顯示你正在嘗試做什麼,不可能知道什麼可能是錯的。 – Claies
昨天發生了同樣的事情,它與郵遞員工作正常,但不是來自API,請發佈一些代碼來看看。 – Wcan
你得到什麼控制檯錯誤? – Rakeschand