0
웹 API를 만들어 localhost에 게시했습니다.http가 작동하는 동안 angularjs에서 http 게시 요청이 작동하지 않습니다.
AngularJS를 사용하여 스크립트 파일을 만든 웹 페이지가 생성되었습니다.
api와 웹 페이지 모두 로컬 호스트에서 실행됩니다.
HTTP 요청을 보내면 JSON 응답을 받지만 Post 메소드를 사용하여 API에 데이터를 추가 할 수 없습니다.
게시물 요청은 우편 배달부에서 완벽하게 작동합니다. 새로운 데이터를 추가합니다.
는 HTTP 포스트에 전송해야합니다 귀하의 모든 데이터와 JSON 모델을 수용하는 엔티티 모델을 만들 필요가 HTTP 포스트를 들어
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